KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > TracePoint2D


1 /*
2  * TracePoint2D, a tuned Point2D.Double for use with ITrace2D- implementations.
3  * Copyright (C) 2002 Achim Westermann, Achim.Westermann@gmx.de
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  */

22
23 package info.monitorenter.gui.chart;
24
25 import info.monitorenter.gui.chart.traces.ATrace2D;
26
27 import java.awt.geom.Point2D JavaDoc;
28
29 /**
30  * A specialized version of <code>java.awt.Point2D.Double </code> who carries
31  * two further values: <code> double scaledX</code> and
32  * <code>double scaledY</code> which allow the <code>Chart2D</code> to cache
33  * the scaled values (between 0.0 and 1.0) without having to keep a copy of the
34  * aggregators (<code>ITrace2D</code>) complete tracepoints.
35  * <p>
36  * This avoids the necessarity to care for the correct order of a set of scaled
37  * tracepoints copied for caching purposes. Especially in the case of new
38  * <code>TracePoint2D</code> instances added to a <code>ITrace2D</code>
39  * instance managed by a <code>Chart2D</code> there remains no responsibility
40  * for sorting the cached copy. This allows that the managing
41  * <code>Chart2D</code> may just rescale the newly added tracepoint instead of
42  * searching for the correct order of the new tracepoint by value - comparisons
43  * of x and y: The <code>TracePoint2D</code> passed to the method
44  * <code>traceChanged(Chart2DDataChangeEvent e)</code> coded in the argument
45  * is the original. <br>
46  * <p>
47  * Why caching of scaled values for the coordinates? <br>
48  * This takes more RAM but else for every <code>repaint()</code> invocation of
49  * the <code>Chart2D</code> would force all tracepoints of all traces to be
50  * rescaled again.
51  * <p>
52  * A TracePoint2D will inform it's listener of type <code>ITrace</code> on
53  * changes of the internal values.
54  * <p>
55  *
56  * @author Achim Westermann <a
57  * HREF='mailto:Achim.Westermann@gmx.de'>Achim.Westermann@gmx.de </a>
58  *
59  * @version $Revision: 1.4 $
60  */

61 public class TracePoint2D extends Point2D.Double JavaDoc implements Comparable JavaDoc, java.io.Serializable JavaDoc,
62     Cloneable JavaDoc {
63
64   /**
65    * Generated <code>serialVersionUID</code>.
66    */

67   private static final long serialVersionUID = 3618980079204512309L;
68
69   /**
70    * The reference to the listening <code>ITrace</code> who owns this point.
71    * <p>
72    *
73    * A trace point should be contained only in one trace!
74    * <p>
75    */

76   private ATrace2D m_listener;
77
78   /**
79    * Flag for the Chart2D painter that allows it to render only instances he has
80    * processed before.
81    */

82   protected boolean m_scaledOnce = false;
83
84   /**
85    * Accessible at package-level for the <code>Chart2D</code>.
86    */

87   protected double m_scaledX;
88
89   /**
90    * Accessible at package-level for the <code>Chart2D</code>.
91    */

92   protected double m_scaledY;
93
94   /**
95    * Construct a TracePoint2D whose coords are initalized to (x,y).
96    * <p>
97    *
98    * @param x
99    * the x value to use.
100    *
101    * @param y
102    * the y value to use.
103    */

104   public TracePoint2D(final double x, final double y) {
105
106     super(x, y);
107   }
108
109   /**
110    * @see java.lang.Object#clone()
111    */

112   public Object JavaDoc clone() {
113     TracePoint2D result = new TracePoint2D(this.x, this.y);
114     result.m_scaledOnce = this.m_scaledOnce;
115     result.m_scaledX = this.m_scaledX;
116     result.m_scaledY = this.m_scaledY;
117     return result;
118   }
119
120   /**
121    * Compares to {@link TracePoint2D} instances by their x value in ascending
122    * order.
123    * <p>
124    *
125    * @param obj
126    * the point to compare to this instance.
127    *
128    * @return -1 if the given point has a higher x value, 0 if it has the same
129    * value or 1 if it has a lower x value.
130    *
131    * @see Comparable#compareTo(java.lang.Object)
132    *
133    * @throws ClassCastException
134    * if the given instance is not of this type.
135    */

136   public int compareTo(final Object JavaDoc obj) throws ClassCastException JavaDoc {
137
138     double othx = ((Point2D.Double JavaDoc) obj).getX();
139     if (this.x < othx) {
140       return -1;
141     }
142     if (this.x == othx) {
143       return 0;
144     } else {
145       return 1;
146     }
147   }
148
149   /**
150    * @see java.lang.Object#equals(java.lang.Object)
151    */

152   public boolean equals(final Object JavaDoc o) {
153     if (o == null) {
154       return false;
155     }
156     return compareTo(o) == 0;
157   }
158
159   /**
160    * @see java.lang.Object#hashCode()
161    */

162   public int hashCode() {
163     return super.hashCode();
164   }
165
166   /**
167    * Allows <code>ITrace2D</code> instances to register (or deregister)
168    * themselves with this point to receive (or stop receiving) change
169    * information via {@link ATrace2D#firePointChanged(TracePoint2D, boolean)}
170    * events.
171    * <p>
172    *
173    * @param listener
174    * The instance that will be informed about changes or null to
175    * deregister.
176    */

177   public void setListener(final ATrace2D listener) {
178     this.m_listener = listener;
179   }
180
181   /**
182    * <p>
183    * This method overloads the method of
184    * <code>java.awt.geom.Point2D.Double</code> to fire a property change event
185    * to listeners of the corresponding <code>{@link ITrace2D}</code> instances
186    * via their method
187    * <code>{@link ATrace2D#firePointChanged(TracePoint2D, boolean)}</code>
188    * (with argument true).
189    * </p>
190    *
191    * @param x
192    * the new x-coordinate for this point.
193    * @param y
194    * the new y-coordinate for this point.
195    */

196   public void setLocation(final double x, final double y) {
197
198     super.setLocation(x, y);
199     if (this.m_listener != null) {
200       this.m_listener.firePointChanged(this, true);
201     }
202   }
203 }
204
Popular Tags