00001 package edu.stanford.hci.r3.paper.regions;
00002
00003 import java.awt.Color;
00004 import java.awt.Polygon;
00005 import java.awt.geom.Point2D;
00006
00007 import edu.stanford.hci.r3.paper.Region;
00008 import edu.stanford.hci.r3.render.RegionRenderer;
00009 import edu.stanford.hci.r3.render.regions.PolygonRenderer;
00010 import edu.stanford.hci.r3.units.Units;
00011 import edu.stanford.hci.r3.util.graphics.GraphicsUtils;
00012
00036 public class PolygonalRegion extends Region {
00037
00042 private static double getMinX(Point2D[] points) {
00043 double minX = Double.MAX_VALUE;
00044 for (Point2D p : points) {
00045 minX = Math.min(minX, p.getX());
00046 }
00047 return minX;
00048 }
00049
00054 private static double getMinY(Point2D[] points) {
00055 double minY = Double.MAX_VALUE;
00056 for (Point2D p : points) {
00057 minY = Math.min(minY, p.getY());
00058 }
00059 return minY;
00060 }
00061
00062 private Units offsetX;
00063
00064 private Units offsetY;
00065
00066 private Color strokeColor = Color.BLACK;
00067
00068 private int strokeThickness = 1;
00069
00076 public PolygonalRegion(String name, Units u, Point2D... points) {
00077 this(name, u, u.getUnitsObjectOfSameTypeWithValue(getMinX(points)), u
00078 .getUnitsObjectOfSameTypeWithValue(getMinY(points)), points);
00079 }
00080
00090 public PolygonalRegion(String name, Units currUnits, Units destUnits, Point2D... pts) {
00091 this(name, destUnits, GraphicsUtils.getPointsWithConvertedUnits(currUnits, destUnits, pts));
00092 }
00093
00107 private PolygonalRegion(String name, Units u, Units originX, Units originY, Point2D... points) {
00108 super(name, GraphicsUtils.createPolygon(points), u);
00109
00110
00111 offsetX = originX;
00112 offsetY = originY;
00113
00114 setName("A Polygonal Region");
00115 }
00116
00120 public Units getOffsetX() {
00121 return offsetX;
00122 }
00123
00127 public Units getOffsetY() {
00128 return offsetY;
00129 }
00130
00134 public Polygon getPolygonReference() {
00135 return (Polygon) getShape();
00136 }
00137
00141 public RegionRenderer getRenderer() {
00142 return new PolygonRenderer(this);
00143 }
00144
00145 public Color getStrokeColor() {
00146 return strokeColor;
00147 }
00148
00149 public int getStrokeThickness() {
00150 return strokeThickness;
00151 }
00152
00153 public void setStrokeColor(Color strokeColor) {
00154 this.strokeColor = strokeColor;
00155 }
00156
00157 public void setStrokeThickness(int strokeThickness) {
00158 this.strokeThickness = strokeThickness;
00159 }
00160
00166 public String toString() {
00167 final StringBuilder sb = new StringBuilder();
00168 sb.append("Polygon: {");
00169 final Polygon poly = getPolygonReference();
00170 final double sX = getScaleX();
00171 final double sY = getScaleY();
00172
00173 for (int i = 0; i < poly.npoints; i++) {
00174 sb.append("(" + poly.xpoints[i] * sX + ", " + poly.ypoints[i] * sY + ")");
00175 }
00176 sb.append("} in " + getUnits().getUnitName());
00177 return sb.toString();
00178 }
00179 }