Open Source Repository

Home /jfreechart/jfreechart-1.0.9 | Repository Home



org/jfree/chart/plot/dial/DialCap.java
/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ------------
 * DialCap.java
 * ------------
 * (C) Copyright 2006, 2007, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * Changes
 * -------
 * 03-Nov-2006 : Version 1 (DG);
 * 17-Oct-2007 : Updated equals() method (DG);
 
 */

package org.jfree.chart.plot.dial;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.jfree.chart.HashUtilities;
import org.jfree.io.SerialUtilities;
import org.jfree.util.PaintUtilities;
import org.jfree.util.PublicCloneable;

/**
 * A regular dial layer that can be used to draw a cap over the center of 
 * the dial (the base of the dial pointer(s)).
 
 @since 1.0.7
 */
public class DialCap extends AbstractDialLayer implements DialLayer, Cloneable, 
        PublicCloneable, Serializable {
    
    /** For serialization. */
    static final long serialVersionUID = -2929484264982524463L;

    /**
     * The radius of the cap, as a percentage of the framing rectangle.
     */
    private double radius;
    
    /** 
     * The fill paint.  This field is transient because it requires special
     * handling for serialization.
     */
    private transient Paint fillPaint;
    
    /** 
     * The paint used to draw the cap outline (this should never be 
     <code>null</code>).  This field is transient because it requires
     * special handling for serialization.
     */
    private transient Paint outlinePaint;
    
    /** 
     * The stroke used to draw the cap outline (this should never be 
     <code>null</code>).   This field is transient because it requires
     * special handling for serialization.
     */
    private transient Stroke outlineStroke;
    
    /** 
     * Creates a new instance of <code>StandardDialBackground</code>.  The 
     * default background paint is <code>Color.white</code>.
     */
    public DialCap() {
        this.radius = 0.05;
        this.fillPaint = Color.white;
        this.outlinePaint = Color.black;
        this.outlineStroke = new BasicStroke(2.0f);
    }
    
    /**
     * Returns the radius of the cap, as a percentage of the dial's framing
     * rectangle.
     *
     @return The radius.
     *
     @see #setRadius(double)
     */
    public double getRadius() {
        return this.radius;
    }
    
    /**
     * Sets the radius of the cap, as a percentage of the dial's framing
     * rectangle, and sends a {@link DialLayerChangeEvent} to all registered
     * listeners.
     *
     @param radius  the radius (must be greater than zero).
     *
     @see #getRadius()
     */
    public void setRadius(double radius) {
        if (radius <= 0.0) {
            throw new IllegalArgumentException("Requires radius > 0.0.");
        }
        this.radius = radius;
        notifyListeners(new DialLayerChangeEvent(this));
    }
    
    /**
     * Returns the paint used to fill the cap. 
     *
     @return The paint (never <code>null</code>).
     *
     @see #setFillPaint(Paint)
     */
    public Paint getFillPaint() {
        return this.fillPaint;
    }
    
    /**
     * Sets the paint for the cap background and sends a 
     {@link DialLayerChangeEvent} to all registered listeners.
     *
     @param paint  the paint (<code>null</code> not permitted).
     *
     @see #getFillPaint()
     */
    public void setFillPaint(Paint paint) {
        if (paint == null) {
            throw new IllegalArgumentException("Null 'paint' argument.");
        }
        this.fillPaint = paint;
        notifyListeners(new DialLayerChangeEvent(this));
    }
        
    /**
     * Returns the paint used to draw the outline of the cap. 
     *
     @return The paint (never <code>null</code>).
     *
     @see #setOutlinePaint(Paint)
     */
    public Paint getOutlinePaint() {
        return this.outlinePaint;
    }
    
    /**
     * Sets the paint used to draw the outline of the cap and sends a 
     {@link DialLayerChangeEvent} to all registered listeners.
     *
     @param paint  the paint (<code>null</code> not permitted).
     *
     @see #getOutlinePaint()
     */
    public void setOutlinePaint(Paint paint) {
        if (paint == null) {
            throw new IllegalArgumentException("Null 'paint' argument.");
        }
        this.outlinePaint = paint;
        notifyListeners(new DialLayerChangeEvent(this));
    }
        
    /**
     * Returns the stroke used to draw the outline of the cap. 
     *
     @return The stroke (never <code>null</code>).
     *
     @see #setOutlineStroke(Stroke)
     */
    public Stroke getOutlineStroke() {
        return this.outlineStroke;
    }
    
    /**
     * Sets the stroke used to draw the outline of the cap and sends a 
     {@link DialLayerChangeEvent} to all registered listeners.
     *
     @param stroke  the stroke (<code>null</code> not permitted).
     *
     @see #getOutlineStroke()
     */
    public void setOutlineStroke(Stroke stroke) {
        if (stroke == null) {
            throw new IllegalArgumentException("Null 'stroke' argument.");
        }
        this.outlineStroke = stroke;
        notifyListeners(new DialLayerChangeEvent(this));
    }
    
    /**
     * Returns <code>true</code> to indicate that this layer should be 
     * clipped within the dial window. 
     *
     @return <code>true</code>.
     */
    public boolean isClippedToWindow() {
        return true;
    }
    
    /**
     * Draws the background to the specified graphics device.  If the dial
     * frame specifies a window, the clipping region will already have been 
     * set to this window before this method is called.
     *
     @param g2  the graphics device (<code>null</code> not permitted).
     @param plot  the plot (ignored here).
     @param frame  the dial frame (ignored here).
     @param view  the view rectangle (<code>null</code> not permitted). 
     */
    public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, 
            Rectangle2D view) {

        g2.setPaint(this.fillPaint);
        
        Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius, 
                this.radius);
        Ellipse2D e = new Ellipse2D.Double(f.getX(), f.getY(), f.getWidth()
                f.getHeight());
        g2.fill(e);
        g2.setPaint(this.outlinePaint);
        g2.setStroke(this.outlineStroke);
        g2.draw(e);
        
    }
    
    /**
     * Tests this instance for equality with an arbitrary object.
     *
     @param obj  the object (<code>null</code> permitted).
     *
     @return A boolean.
     */
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof DialCap)) {
            return false;
        }
        DialCap that = (DialCapobj;
        if (this.radius != that.radius) {
            return false;
        }
        if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
            return false;
        }
        if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
            return false;
        }
        if (!this.outlineStroke.equals(that.outlineStroke)) {
            return false;
        }
        return super.equals(obj);
    }
    
    /**
     * Returns a hash code for this instance.
     
     @return The hash code.
     */
    public int hashCode() {
        int result = 193;
        result = 37 * result + HashUtilities.hashCodeForPaint(this.fillPaint);
        result = 37 * result + HashUtilities.hashCodeForPaint(
                this.outlinePaint);
        result = 37 * result + this.outlineStroke.hashCode();
        return result;
    }
    
    /**
     * Returns a clone of this instance.
     *
     @return A clone.
     
     @throws CloneNotSupportedException if some attribute of the cap cannot
     *     be cloned.
     */
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    /**
     * Provides serialization support.
     *
     @param stream  the output stream.
     *
     @throws IOException  if there is an I/O error.
     */
    private void writeObject(ObjectOutputStream streamthrows IOException {
        stream.defaultWriteObject();
        SerialUtilities.writePaint(this.fillPaint, stream);
        SerialUtilities.writePaint(this.outlinePaint, stream);
        SerialUtilities.writeStroke(this.outlineStroke, stream);
    }

    /**
     * Provides serialization support.
     *
     @param stream  the input stream.
     *
     @throws IOException  if there is an I/O error.
     @throws ClassNotFoundException  if there is a classpath problem.
     */
    private void readObject(ObjectInputStream stream
            throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        this.fillPaint = SerialUtilities.readPaint(stream);
        this.outlinePaint = SerialUtilities.readPaint(stream);
        this.outlineStroke = SerialUtilities.readStroke(stream);
    }
    
}