001package myproject.model.swing; 002 003/** 004 * A translator from relative model space to screen graphics. 005 */ 006abstract class Translator { 007 double tX; 008 double tY; 009 double tWidth; 010 double tHeight; 011 double tScaleFactor; 012 Translator(double tX, double tY, double tWidth, double tHeight, double tScaleFactor) { 013 this.tX = tX; 014 this.tY = tY; 015 this.tWidth = tWidth; 016 this.tHeight = tHeight; 017 this.tScaleFactor = tScaleFactor; 018 } 019 int scale(double arg) { 020 return (int) Math.ceil(arg * tScaleFactor); 021 } 022 abstract int getX(double x, double y, double width, double height); 023 abstract int getY(double x, double y, double width, double height); 024 abstract int getWidth(double width, double height); 025 abstract int getHeight(double width, double height); 026} 027 028class TranslatorWE extends Translator { 029 TranslatorWE(double tX, double tY, double tWidth, double tHeight, double tScaleFactor) { 030 super(tX, tY, tWidth, tHeight, tScaleFactor); 031 } 032 int getX(double x, double y, double width, double height) { return scale(tX+x); } 033 int getY(double x, double y, double width, double height) { return scale(tY+y); } 034 int getWidth(double width, double height) { return scale(width); } 035 int getHeight(double width, double height) { return scale(height); } 036} 037 038class TranslatorEW extends Translator { 039 TranslatorEW(double tX, double tY, double tWidth, double tHeight, double tScaleFactor) { 040 super(tX, tY, tWidth, tHeight, tScaleFactor); 041 } 042 int getX(double x, double y, double width, double height) { return scale(tX+tWidth-x-width); } 043 int getY(double x, double y, double width, double height) { return scale(tY+tHeight-y-height); } 044 int getWidth(double width, double height) { return scale(width); } 045 int getHeight(double width, double height) { return scale(height); } 046} 047 048class TranslatorNS extends Translator { 049 TranslatorNS(double tX, double tY, double tWidth, double tHeight, double tScaleFactor) { 050 super(tX, tY, tWidth, tHeight, tScaleFactor); 051 } 052 int getX(double x, double y, double width, double height) { return scale(tX+y); } 053 int getY(double x, double y, double width, double height) { return scale(tY+x); } 054 int getWidth(double width, double height) { return scale(height); } 055 int getHeight(double width, double height) { return scale(width); } 056} 057 058class TranslatorSN extends Translator { 059 TranslatorSN(double tX, double tY, double tWidth, double tHeight, double tScaleFactor) { 060 super(tX, tY, tWidth, tHeight, tScaleFactor); 061 } 062 int getX(double x, double y, double width, double height) { return scale(tX+tHeight-y-height); } 063 int getY(double x, double y, double width, double height) { return scale(tY+tWidth-x-width); } 064 int getWidth(double width, double height) { return scale(height); } 065 int getHeight(double width, double height) { return scale(width); } 066} 067