Source for org.jfree.chart.needle.ArrowNeedle

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * ArrowNeedle.java
  29:  * ----------------
  30:  * (C) Copyright 2002-2007, 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:  * Changes:
  37:  * --------
  38:  * 25-Sep-2002 : Version 1, contributed by Bryan Scott (DG);
  39:  * 27-Mar-2003 : Implemented Serializable (DG);
  40:  * 09-Sep-2003 : Added equals() method (DG);
  41:  * 08-Jun-2005 : Implemented Cloneable (DG);
  42:  * 22-Nov-2007 : Added hashCode() implementation (DG);
  43:  * 
  44:  */
  45: 
  46: package org.jfree.chart.needle;
  47: 
  48: import java.awt.Graphics2D;
  49: import java.awt.Shape;
  50: import java.awt.geom.GeneralPath;
  51: import java.awt.geom.Line2D;
  52: import java.awt.geom.Point2D;
  53: import java.awt.geom.Rectangle2D;
  54: import java.io.Serializable;
  55: 
  56: import org.jfree.chart.HashUtilities;
  57: 
  58: /**
  59:  * A needle in the shape of an arrow.
  60:  */
  61: public class ArrowNeedle extends MeterNeedle 
  62:                          implements Cloneable, Serializable {
  63: 
  64:     /** For serialization. */
  65:     private static final long serialVersionUID = -5334056511213782357L;
  66:     
  67:     /** 
  68:      * A flag controlling whether or not there is an arrow at the top of the 
  69:      * needle. 
  70:      */
  71:     private boolean isArrowAtTop = true;
  72: 
  73:     /**
  74:      * Constructs a new arrow needle.
  75:      *
  76:      * @param isArrowAtTop  a flag that controls whether or not there is an 
  77:      *                      arrow at the top of the needle.
  78:      */
  79:     public ArrowNeedle(boolean isArrowAtTop) {
  80:         this.isArrowAtTop = isArrowAtTop;
  81:     }
  82: 
  83:     /**
  84:      * Draws the needle.
  85:      *
  86:      * @param g2  the graphics device.
  87:      * @param plotArea  the plot area.
  88:      * @param rotate  the rotation point.
  89:      * @param angle  the angle.
  90:      */
  91:     protected void drawNeedle(Graphics2D g2, Rectangle2D plotArea, 
  92:                               Point2D rotate, double angle) {
  93: 
  94:         Line2D shape = new Line2D.Float();
  95:         Shape d = null;
  96: 
  97:         float x = (float) (plotArea.getMinX() +  (plotArea.getWidth() / 2));
  98:         float minY = (float) plotArea.getMinY();
  99:         float maxY = (float) plotArea.getMaxY();
 100:         shape.setLine(x, minY, x, maxY);
 101: 
 102:         GeneralPath shape1 = new GeneralPath();
 103:         if (this.isArrowAtTop) {
 104:             shape1.moveTo(x, minY);
 105:             minY += 4 * getSize();
 106:         }
 107:         else {
 108:             shape1.moveTo(x, maxY);
 109:             minY = maxY - 4 * getSize();
 110:         }
 111:         shape1.lineTo(x + getSize(), minY);
 112:         shape1.lineTo(x - getSize(), minY);
 113:         shape1.closePath();
 114: 
 115:         if ((rotate != null) && (angle != 0)) {
 116:             getTransform().setToRotation(angle, rotate.getX(), rotate.getY());
 117:             d = getTransform().createTransformedShape(shape);
 118:         }
 119:         else {
 120:             d = shape;
 121:         }
 122:         defaultDisplay(g2, d);
 123: 
 124:         if ((rotate != null) && (angle != 0)) {
 125:             d = getTransform().createTransformedShape(shape1);
 126:         }
 127:         else {
 128:             d = shape1;
 129:         }
 130:         defaultDisplay(g2, d);
 131: 
 132:     }
 133: 
 134:     /**
 135:      * Tests another object for equality with this object.
 136:      * 
 137:      * @param obj  the object to test (<code>null</code> permitted).
 138:      * 
 139:      * @return A boolean.
 140:      */
 141:     public boolean equals(Object obj) {
 142:         if (obj == this) {
 143:             return true;
 144:         }
 145:         if (!(obj instanceof ArrowNeedle)) {
 146:             return false; 
 147:         }
 148:         if (!super.equals(obj)) {
 149:             return false;
 150:         }
 151:         ArrowNeedle that = (ArrowNeedle) obj;
 152:         if (this.isArrowAtTop != that.isArrowAtTop) {
 153:             return false;
 154:         }
 155:         return true;
 156:     }
 157:     
 158:     /**
 159:      * Returns a hash code for this instance.
 160:      * 
 161:      * @return A hash code.
 162:      */
 163:     public int hashCode() {
 164:         int result = super.hashCode();
 165:         result = HashUtilities.hashCode(result, this.isArrowAtTop);
 166:         return result; 
 167:     }
 168: 
 169:     /**
 170:      * Returns a clone of this needle.
 171:      * 
 172:      * @return A clone.
 173:      * 
 174:      * @throws CloneNotSupportedException if the <code>ArrowNeedle</code> 
 175:      *     cannot be cloned (in theory, this should not happen).
 176:      */
 177:     public Object clone() throws CloneNotSupportedException {
 178:         return super.clone();   
 179:     }
 180:     
 181: }