summaryrefslogtreecommitdiff
path: root/NET/worlds/console/MoveablePolygon.java
blob: 7607a4f6a5ca76047fd76bf75335e276556d9bf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
   }
}