1 19 24 25 package org.netbeans.swing.tabcontrol.plaf; 26 27 import java.awt.*; 28 import java.util.Arrays ; 29 import java.util.Comparator ; 30 import java.util.HashSet ; 31 32 33 46 public final class EqualPolygon extends Polygon { 47 48 51 public EqualPolygon() { 52 } 53 54 58 public EqualPolygon(int[] x, int[] y, int n) { 59 xpoints = new int[n]; 62 ypoints = new int[n]; 63 System.arraycopy(x, 0, xpoints, 0, xpoints.length); 64 System.arraycopy(y, 0, ypoints, 0, ypoints.length); 65 npoints = n; 66 } 67 68 75 public EqualPolygon(Polygon p) { 76 super(p.xpoints, p.ypoints, p.npoints); 77 } 78 79 80 public EqualPolygon(Rectangle r) { 81 super ( 82 new int[] {r.x, r.x + r.width, r.x + r.width, r.x}, 83 new int[] {r.y, r.y, r.y + r.height, r.y + r.height}, 84 4 85 ); 86 } 87 88 92 public EqualPolygon(int[] x, int[] y) { 93 super(x, y, x.length); 94 } 95 96 102 public void moveTo(int x, int y) { 103 addPoint(x, y); 104 } 105 106 112 public void lineTo(int x, int y) { 113 addPoint(x, y); 114 } 115 116 122 public Object clone() { 123 return new EqualPolygon(xpoints, ypoints, xpoints.length); 124 } 125 126 131 public String toString() { 132 StringBuffer sb = new StringBuffer ("EqualPolygon: "); for (int i = 0; i < npoints; i++) { 134 sb.append(' '); sb.append(xpoints[i]); 136 sb.append(','); sb.append(ypoints[i]); 138 } 139 return sb.toString(); 140 } 141 142 147 public int hashCode() { 148 return arrayHashCode(xpoints) ^ arrayHashCode(ypoints); 149 } 150 151 private int arrayHashCode(int[] o) { 152 int result = 0; 153 for (int i = 0; i < npoints; i++) { 154 result += o[i] ^ i; 155 } 156 return result; 157 } 158 159 166 public boolean equals(Object o) { 167 if (o == this) { 168 return true; 169 } 170 if (o instanceof Polygon) { 171 Polygon p = (Polygon) o; 172 int[] ox = p.xpoints; 173 int[] oy = p.ypoints; 174 boolean result = Arrays.equals(xpoints, ox) 175 && Arrays.equals(ypoints, oy); 176 result &= p.npoints == npoints; 177 return result; 178 } else { 179 return false; 180 } 181 } 182 183 private Point[] sortPoints(Point[] p) { 184 HashSet <Point> set = new HashSet <Point>(Arrays.asList(p)); 186 p = new Point[set.size()]; 187 p = set.toArray(p); 188 Arrays.sort(p, comparator); 190 return p; 191 } 192 193 private static final Comparator <Point> comparator = new PointsComparator(); 194 195 private static class PointsComparator implements Comparator <Point> { 196 public int compare(Point a, Point b) { 197 int result = (a.y * (a.x - b.x)) - (b.y * (b.x - a.x)); 198 return result; 199 } 200 } 201 202 } 203 | Popular Tags |