diff options
Diffstat (limited to 'NET/worlds/console/MoveablePolygon.java')
| -rw-r--r-- | NET/worlds/console/MoveablePolygon.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/NET/worlds/console/MoveablePolygon.java b/NET/worlds/console/MoveablePolygon.java new file mode 100644 index 0000000..7607a4f --- /dev/null +++ b/NET/worlds/console/MoveablePolygon.java @@ -0,0 +1,58 @@ +package NET.worlds.console; + +import java.awt.Graphics; +import java.awt.Polygon; +import java.awt.Rectangle; + +class MoveablePolygon extends Polygon { + private static final long serialVersionUID = 8800634603983512717L; + private int xOffset; + private int yOffset; + private Rectangle boundingBox = new Rectangle(); + + public MoveablePolygon() { + } + + public MoveablePolygon(int[] xpoints, int[] ypoints) { + super(xpoints, ypoints, xpoints.length); + } + + public void moveTo(int x, int y) { + if (x != this.xOffset || y != this.yOffset) { + int xdelta = x - this.xOffset; + int ydelta = y - this.yOffset; + this.xOffset = x; + this.yOffset = y; + + for (int i = 0; i < this.npoints; i++) { + this.xpoints[i] = this.xpoints[i] + xdelta; + this.ypoints[i] = this.ypoints[i] + ydelta; + } + } + } + + public void drawFilled(Graphics g, int x, int y) { + this.moveTo(x, y); + g.fillPolygon(this); + } + + @Override + public Rectangle getBoundingBox() { + int boundsMinX = Integer.MAX_VALUE; + int boundsMinY = Integer.MAX_VALUE; + int boundsMaxX = Integer.MIN_VALUE; + int boundsMaxY = Integer.MIN_VALUE; + + for (int i = 0; i < this.npoints; i++) { + int x = this.xpoints[i]; + boundsMinX = Math.min(boundsMinX, x); + boundsMaxX = Math.max(boundsMaxX, x); + int y = this.ypoints[i]; + boundsMinY = Math.min(boundsMinY, y); + boundsMaxY = Math.max(boundsMaxY, y); + } + + this.boundingBox.reshape(boundsMinX, boundsMinY, boundsMaxX - boundsMinX, boundsMaxY - boundsMinY); + return this.boundingBox; + } +} |