KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > EqualPolygon


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * EqualPolygon.java
21  *
22  * Created on February 10, 2004, 1:17 PM
23  */

24
25 package org.netbeans.swing.tabcontrol.plaf;
26
27 import java.awt.*;
28 import java.util.Arrays JavaDoc;
29 import java.util.Comparator JavaDoc;
30 import java.util.HashSet JavaDoc;
31
32
33 /**
34  * A Polygon which implements a proper equals/hashcode contract. In order to
35  * optimize drag and drop repainting, it is necessary that the Shape objects
36  * returned by getTabIndication() be able to be compared properly.
37  * <p/>
38  * To ease migration of older code, this class also implements a couple methods
39  * of GeneralPath, which was used before. These methods just delegate to
40  * addPoint(), so the full functionality of GeneralPath is not replicated
41  * (specifically, a polygon must be contiguous and closed).
42  * <p/>
43  *
44  * @author Tim Boudreau
45  */

46 public final class EqualPolygon extends Polygon {
47
48     /**
49      * Creates a new instance of EqualGeneralPath
50      */

51     public EqualPolygon() {
52     }
53
54     /**
55      * Copy constructor will copy the xpoints/ypoints arrays so the caller can
56      * later modify them without changing the polygon constructor here.
57      */

58     public EqualPolygon(int[] x, int[] y, int n) {
59         //Must clone the arrays, or transforms on the source of the polygon
60
//will also transform this one
61
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     /**
69      * Copy constructor - takes either another EqualPolygon or a Polygon. Copies
70      * the points arrays of the original polygon, so the passed polygon may be
71      * modified without affecting the instance constructed here.
72      *
73      * @param p
74      */

75     public EqualPolygon(Polygon p) {
76         super(p.xpoints, p.ypoints, p.npoints);
77     }
78
79     /** Convenience constructor which takes a Rectangle */
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     /**
89      * Non copy constructor based on fixed arrays. Takes the point count
90      * parameter from<code>x.length</code>.
91      */

92     public EqualPolygon(int[] x, int[] y) {
93         super(x, y, x.length);
94     }
95
96     /**
97      * Delegates to <code>Polygon.addPoint()</code>.
98      *
99      * @param x x coordinate
100      * @param y y coordinate
101      */

102     public void moveTo(int x, int y) {
103         addPoint(x, y);
104     }
105
106     /**
107      * Delegates to <code>Polygon.addPoint()</code>.
108      *
109      * @param x x coordinate
110      * @param y y coordinate
111      */

112     public void lineTo(int x, int y) {
113         addPoint(x, y);
114     }
115
116     /**
117      * Creates a new EqualPolygon using the copy constructor - the resulting
118      * polygon may be modified without affecting the original.
119      *
120      * @return A new instance of EqualPolygon with the same point values
121      */

122     public Object JavaDoc clone() {
123         return new EqualPolygon(xpoints, ypoints, xpoints.length);
124     }
125
126     /**
127      * Overridden to produce a meaningful result.
128      *
129      * @return A string representation of the EqualPolygon
130      */

131     public String JavaDoc toString() {
132         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("EqualPolygon: "); //NOI18N
133
for (int i = 0; i < npoints; i++) {
134             sb.append(' '); //NOI18N
135
sb.append(xpoints[i]);
136             sb.append(','); //NOI18N
137
sb.append(ypoints[i]);
138         }
139         return sb.toString();
140     }
141
142     /**
143      * Computes a hashCode based on the points arrays.
144      *
145      * @return The hash code
146      */

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     /**
160      * Returns true if the argument is a Polygon (does not need to be
161      * EqualPolygon) and its point arrays and number of points matches.
162      *
163      * @param o Another polygon
164      * @return whether or not they are equal
165      */

166     public boolean equals(Object JavaDoc 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         //Prune duplicates
185
HashSet JavaDoc<Point> set = new HashSet JavaDoc<Point>(Arrays.asList(p));
186         p = new Point[set.size()];
187         p = set.toArray(p);
188         //Then sort
189
Arrays.sort(p, comparator);
190         return p;
191     }
192
193     private static final Comparator JavaDoc<Point> comparator = new PointsComparator();
194
195     private static class PointsComparator implements Comparator JavaDoc<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