KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > needle > MiddlePinNeedle


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------
28  * PinNeedle.java
29  * --------------
30  * (C) Copyright 2002-2006, by the Australian Antarctic Division and
31  * Contributors.
32  *
33  * Original Author: Bryan Scott (for the Australian Antarctic Division);
34  * Contributor(s): David Gilbert (for Object Refinery Limited);
35  *
36  * $Id: MiddlePinNeedle.java,v 1.4.2.3 2006/08/02 10:40:57 mungady Exp $
37  *
38  * Changes:
39  * --------
40  * 25-Sep-2002 : Version 1, contributed by Bryan Scott (DG);
41  * 27-Mar-2003 : Implemented Serializable (DG);
42  * 09-Sep-2003 : Added equals() method (DG);
43  * 08-Jun-2005 : Implemented Cloneable (DG);
44  *
45  */

46
47 package org.jfree.chart.needle;
48
49 import java.awt.Graphics2D JavaDoc;
50 import java.awt.geom.Area JavaDoc;
51 import java.awt.geom.Ellipse2D JavaDoc;
52 import java.awt.geom.GeneralPath JavaDoc;
53 import java.awt.geom.Point2D JavaDoc;
54 import java.awt.geom.Rectangle2D JavaDoc;
55 import java.io.Serializable JavaDoc;
56
57 /**
58  * A needle that is drawn as a pin shape.
59  */

60 public class MiddlePinNeedle extends MeterNeedle
61                              implements Cloneable JavaDoc, Serializable JavaDoc {
62
63     /** For serialization. */
64     private static final long serialVersionUID = 6237073996403125310L;
65     
66     /**
67      * Draws the needle.
68      *
69      * @param g2 the graphics device.
70      * @param plotArea the plot area.
71      * @param rotate the rotation point.
72      * @param angle the angle.
73      */

74     protected void drawNeedle(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea,
75                               Point2D JavaDoc rotate, double angle) {
76
77         Area JavaDoc shape;
78         GeneralPath JavaDoc pointer = new GeneralPath JavaDoc();
79
80         int minY = (int) (plotArea.getMinY());
81         //int maxX = (int) (plotArea.getMaxX());
82
int maxY = (int) (plotArea.getMaxY());
83         int midY = ((maxY - minY) / 2) + minY;
84
85         int midX = (int) (plotArea.getMinX() + (plotArea.getWidth() / 2));
86         //int midY = (int) (plotArea.getMinY() + (plotArea.getHeight() / 2));
87
int lenX = (int) (plotArea.getWidth() / 10);
88         if (lenX < 2) {
89             lenX = 2;
90         }
91
92         pointer.moveTo(midX - lenX, midY - lenX);
93         pointer.lineTo(midX + lenX, midY - lenX);
94         pointer.lineTo(midX, minY);
95         pointer.closePath();
96
97         lenX = 4 * lenX;
98         Ellipse2D JavaDoc circle = new Ellipse2D.Double JavaDoc(midX - lenX / 2,
99                                                 midY - lenX, lenX, lenX);
100
101         shape = new Area JavaDoc(circle);
102         shape.add(new Area JavaDoc(pointer));
103         if ((rotate != null) && (angle != 0)) {
104             /// we have rotation
105
getTransform().setToRotation(angle, rotate.getX(), rotate.getY());
106             shape.transform(getTransform());
107         }
108
109         defaultDisplay(g2, shape);
110
111     }
112
113     /**
114      * Tests another object for equality with this object.
115      *
116      * @param object the object to test.
117      *
118      * @return A boolean.
119      */

120     public boolean equals(Object JavaDoc object) {
121         if (object == null) {
122             return false;
123         }
124         if (object == this) {
125             return true;
126         }
127         if (super.equals(object) && object instanceof MiddlePinNeedle) {
128             return true;
129         }
130         return false;
131     }
132     
133     /**
134      * Returns a clone of this needle.
135      *
136      * @return A clone.
137      *
138      * @throws CloneNotSupportedException if the <code>MiddlePinNeedle</code>
139      * cannot be cloned (in theory, this should not happen).
140      */

141     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
142         return super.clone();
143     }
144
145 }
146
Popular Tags