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 * ArrowNeedle.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.Line2D;
044import java.awt.geom.Point2D;
045import java.awt.geom.Rectangle2D;
046import java.io.Serializable;
047
048import org.jfree.chart.HashUtils;
049
050/**
051 * A needle in the shape of an arrow.
052 */
053public class ArrowNeedle extends MeterNeedle implements Cloneable, 
054        Serializable {
055
056    /** For serialization. */
057    private static final long serialVersionUID = -5334056511213782357L;
058
059    /**
060     * A flag controlling whether or not there is an arrow at the top of the
061     * needle.
062     */
063    private boolean isArrowAtTop = true;
064
065    /**
066     * Constructs a new arrow needle.
067     *
068     * @param isArrowAtTop  a flag that controls whether or not there is an
069     *     arrow at the top of the needle.
070     */
071    public ArrowNeedle(boolean isArrowAtTop) {
072        this.isArrowAtTop = isArrowAtTop;
073    }
074
075    /**
076     * Draws the needle.
077     *
078     * @param g2  the graphics device.
079     * @param plotArea  the plot area.
080     * @param rotate  the rotation point.
081     * @param angle  the angle.
082     */
083    @Override
084    protected void drawNeedle(Graphics2D g2, Rectangle2D plotArea,
085            Point2D rotate, double angle) {
086
087        Line2D shape = new Line2D.Float();
088        Shape d;
089
090        float x = (float) (plotArea.getMinX() + (plotArea.getWidth() / 2));
091        float minY = (float) plotArea.getMinY();
092        float maxY = (float) plotArea.getMaxY();
093        shape.setLine(x, minY, x, maxY);
094
095        GeneralPath shape1 = new GeneralPath();
096        if (this.isArrowAtTop) {
097            shape1.moveTo(x, minY);
098            minY += 4 * getSize();
099        } else {
100            shape1.moveTo(x, maxY);
101            minY = maxY - 4 * getSize();
102        }
103        shape1.lineTo(x + getSize(), minY);
104        shape1.lineTo(x - getSize(), minY);
105        shape1.closePath();
106
107        if ((rotate != null) && (angle != 0)) {
108            getTransform().setToRotation(angle, rotate.getX(), rotate.getY());
109            d = getTransform().createTransformedShape(shape);
110        } else {
111            d = shape;
112        }
113        defaultDisplay(g2, d);
114
115        if ((rotate != null) && (angle != 0)) {
116            d = getTransform().createTransformedShape(shape1);
117        } else {
118            d = shape1;
119        }
120        defaultDisplay(g2, d);
121
122    }
123
124    /**
125     * Tests another object for equality with this object.
126     *
127     * @param obj  the object to test ({@code null} permitted).
128     *
129     * @return A boolean.
130     */
131    @Override
132    public boolean equals(Object obj) {
133        if (obj == this) {
134            return true;
135        }
136        if (!(obj instanceof ArrowNeedle)) {
137            return false;
138        }
139        if (!super.equals(obj)) {
140            return false;
141        }
142        ArrowNeedle that = (ArrowNeedle) obj;
143        if (this.isArrowAtTop != that.isArrowAtTop) {
144            return false;
145        }
146        return true;
147    }
148
149    /**
150     * Returns a hash code for this instance.
151     *
152     * @return A hash code.
153     */
154    @Override
155    public int hashCode() {
156        int result = super.hashCode();
157        result = HashUtils.hashCode(result, this.isArrowAtTop);
158        return result;
159    }
160
161    /**
162     * Returns a clone of this needle.
163     *
164     * @return A clone.
165     *
166     * @throws CloneNotSupportedException if the {@code ArrowNeedle}
167     *     cannot be cloned (in theory, this should not happen).
168     */
169    @Override
170    public Object clone() throws CloneNotSupportedException {
171        return super.clone();
172    }
173
174}