001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------
028 * LongNeedle.java
029 * ---------------
030 * (C) Copyright 2002-present, by the Australian Antarctic Division and
031 *                          Contributors.
032 *
033 * Original Author:  Bryan Scott (for the Australian Antarctic Division);
034 * Contributor(s):   David Gilbert;
035 *
036 */
037
038package org.jfree.chart.needle;
039
040import java.awt.Graphics2D;
041import java.awt.Shape;
042import java.awt.geom.GeneralPath;
043import java.awt.geom.Point2D;
044import java.awt.geom.Rectangle2D;
045import java.io.Serializable;
046
047/**
048 * A needle that is represented by a long line.
049 */
050public class LongNeedle extends MeterNeedle implements Cloneable, Serializable {
051
052    /** For serialization. */
053    private static final long serialVersionUID = -4319985779783688159L;
054
055    /**
056     * Default constructor.
057     */
058    public LongNeedle() {
059        super();
060        setRotateY(0.8);
061    }
062
063    /**
064     * Draws the needle.
065     *
066     * @param g2  the graphics device.
067     * @param plotArea  the plot area.
068     * @param rotate  the rotation point.
069     * @param angle  the angle.
070     */
071    @Override
072    protected void drawNeedle(Graphics2D g2, Rectangle2D plotArea,
073            Point2D rotate, double angle) {
074
075        GeneralPath shape1 = new GeneralPath();
076        GeneralPath shape2 = new GeneralPath();
077        GeneralPath shape3 = new GeneralPath();
078
079        float minX = (float) plotArea.getMinX();
080        float minY = (float) plotArea.getMinY();
081        float maxX = (float) plotArea.getMaxX();
082        float maxY = (float) plotArea.getMaxY();
083        //float midX = (float) (minX + (plotArea.getWidth() * getRotateX()));
084        //float midY = (float) (minY + (plotArea.getHeight() * getRotateY()));
085        float midX = (float) (minX + (plotArea.getWidth() * 0.5));
086        float midY = (float) (minY + (plotArea.getHeight() * 0.8));
087        float y = maxY - (2 * (maxY - midY));
088        if (y < minY) {
089            y = minY;
090        }
091        shape1.moveTo(minX, midY);
092        shape1.lineTo(midX, minY);
093        shape1.lineTo(midX, y);
094        shape1.closePath();
095
096        shape2.moveTo(maxX, midY);
097        shape2.lineTo(midX, minY);
098        shape2.lineTo(midX, y);
099        shape2.closePath();
100
101        shape3.moveTo(minX, midY);
102        shape3.lineTo(midX, maxY);
103        shape3.lineTo(maxX, midY);
104        shape3.lineTo(midX, y);
105        shape3.closePath();
106
107        Shape s1 = shape1;
108        Shape s2 = shape2;
109        Shape s3 = shape3;
110
111        if ((rotate != null) && (angle != 0)) {
112            /// we have rotation huston, please spin me
113            getTransform().setToRotation(angle, rotate.getX(), rotate.getY());
114            s1 = shape1.createTransformedShape(transform);
115            s2 = shape2.createTransformedShape(transform);
116            s3 = shape3.createTransformedShape(transform);
117        }
118
119        if (getHighlightPaint() != null) {
120            g2.setPaint(getHighlightPaint());
121            g2.fill(s3);
122        }
123
124        if (getFillPaint() != null) {
125            g2.setPaint(getFillPaint());
126            g2.fill(s1);
127            g2.fill(s2);
128        }
129
130        if (getOutlinePaint() != null) {
131            g2.setStroke(getOutlineStroke());
132            g2.setPaint(getOutlinePaint());
133            g2.draw(s1);
134            g2.draw(s2);
135            g2.draw(s3);
136        }
137    }
138
139    /**
140     * Tests another object for equality with this object.
141     *
142     * @param obj  the object to test ({@code null} permitted).
143     *
144     * @return A boolean.
145     */
146    @Override
147    public boolean equals(Object obj) {
148        if (obj == this) {
149            return true;
150        }
151        if (!(obj instanceof LongNeedle)) {
152            return false;
153        }
154        return super.equals(obj);
155    }
156
157    /**
158     * Returns a hash code for this instance.
159     *
160     * @return A hash code.
161     */
162    @Override
163    public int hashCode() {
164        return super.hashCode();
165    }
166
167    /**
168     * Returns a clone of this needle.
169     *
170     * @return A clone.
171     *
172     * @throws CloneNotSupportedException if the {@code LongNeedle}
173     *     cannot be cloned (in theory, this should not happen).
174     */
175    @Override
176    public Object clone() throws CloneNotSupportedException {
177        return super.clone();
178    }
179
180}