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 * LineBorder.java
029 * ---------------
030 * (C) Copyright 2007-present, by Christo Zietsman and Contributors.
031 *
032 * Original Author:  Christo Zietsman;
033 * Contributor(s):   David Gilbert;
034 *                   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
035 *
036 */
037
038package org.jfree.chart.block;
039
040import java.awt.BasicStroke;
041import java.awt.Color;
042import java.awt.Graphics2D;
043import java.awt.Paint;
044import java.awt.RenderingHints;
045import java.awt.Stroke;
046import java.awt.geom.Line2D;
047import java.awt.geom.Rectangle2D;
048import java.io.IOException;
049import java.io.ObjectInputStream;
050import java.io.ObjectOutputStream;
051import java.io.Serializable;
052import java.util.Objects;
053import org.jfree.chart.HashUtils;
054import org.jfree.chart.ui.RectangleInsets;
055import org.jfree.chart.util.PaintUtils;
056import org.jfree.chart.util.Args;
057import org.jfree.chart.util.SerialUtils;
058
059/**
060 * A line border for any {@link AbstractBlock}.
061 */
062public class LineBorder implements BlockFrame, Serializable {
063
064    /** For serialization. */
065    static final long serialVersionUID = 4630356736707233924L;
066
067    /** The line color. */
068    private transient Paint paint;
069
070    /** The line stroke. */
071    private transient Stroke stroke;
072
073    /** The insets. */
074    private RectangleInsets insets;
075
076    /**
077     * Creates a default border.
078     */
079    public LineBorder() {
080        this(Color.BLACK, new BasicStroke(1.0f), new RectangleInsets(1.0, 1.0,
081                1.0, 1.0));
082    }
083
084    /**
085     * Creates a new border with the specified color.
086     *
087     * @param paint  the color ({@code null} not permitted).
088     * @param stroke  the border stroke ({@code null} not permitted).
089     * @param insets  the insets ({@code null} not permitted).
090     */
091    public LineBorder(Paint paint, Stroke stroke, RectangleInsets insets) {
092        Args.nullNotPermitted(paint, "paint");
093        Args.nullNotPermitted(stroke, "stroke");
094        Args.nullNotPermitted(insets, "insets");
095        this.paint = paint;
096        this.stroke = stroke;
097        this.insets = insets;
098    }
099
100    /**
101     * Returns the paint.
102     *
103     * @return The paint (never {@code null}).
104     */
105    public Paint getPaint() {
106        return this.paint;
107    }
108
109    /**
110     * Returns the insets.
111     *
112     * @return The insets (never {@code null}).
113     */
114    @Override
115    public RectangleInsets getInsets() {
116        return this.insets;
117    }
118
119    /**
120     * Returns the stroke.
121     *
122     * @return The stroke (never {@code null}).
123     */
124    public Stroke getStroke() {
125        return this.stroke;
126    }
127
128    /**
129     * Draws the border by filling in the reserved space (in black).
130     *
131     * @param g2  the graphics device.
132     * @param area  the area.
133     */
134    @Override
135    public void draw(Graphics2D g2, Rectangle2D area) {
136        double w = area.getWidth();
137        double h = area.getHeight();
138        // if the area has zero height or width, we shouldn't draw anything
139        if (w <= 0.0 || h <= 0.0) {
140            return;
141        }
142        double t = this.insets.calculateTopInset(h);
143        double b = this.insets.calculateBottomInset(h);
144        double l = this.insets.calculateLeftInset(w);
145        double r = this.insets.calculateRightInset(w);
146        double x = area.getX();
147        double y = area.getY();
148        double x0 = x + l / 2.0;
149        double x1 = x + w - r / 2.0;
150        double y0 = y + h - b / 2.0;
151        double y1 = y + t / 2.0;
152        g2.setPaint(getPaint());
153        g2.setStroke(getStroke());
154        Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
155        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
156                RenderingHints.VALUE_STROKE_NORMALIZE);
157        Line2D line = new Line2D.Double();
158        if (t > 0.0) {
159            line.setLine(x0, y1, x1, y1);
160            g2.draw(line);
161        }
162        if (b > 0.0) {
163            line.setLine(x0, y0, x1, y0);
164            g2.draw(line);
165        }
166        if (l > 0.0) {
167            line.setLine(x0, y0, x0, y1);
168            g2.draw(line);
169        }
170        if (r > 0.0) {
171            line.setLine(x1, y0, x1, y1);
172            g2.draw(line);
173        }
174        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved);
175    }
176
177    /**
178     * Tests this border for equality with an arbitrary instance.
179     *
180     * @param obj  the object ({@code null} permitted).
181     *
182     * @return A boolean.
183     */
184    @Override
185    public boolean equals(Object obj) {
186        if (obj == this) {
187            return true;
188        }
189        if (!(obj instanceof LineBorder)) {
190            return false;
191        }
192        LineBorder that = (LineBorder) obj;
193        if (!PaintUtils.equal(this.paint, that.paint)) {
194            return false;
195        }
196        if (!Objects.equals(this.stroke, that.stroke)) {
197            return false;
198        }
199        if (!Objects.equals(this.insets, that.insets)) {
200            return false;
201        }
202        return true;
203    }
204
205    @Override
206    public int hashCode() {
207        int hash = 3;
208        hash = 47 * hash + HashUtils.hashCodeForPaint(this.paint);
209        hash = 47 * hash + Objects.hashCode(this.stroke);
210        hash = 47 * hash + Objects.hashCode(this.insets);
211        return hash;
212    }
213
214    /**
215     * Provides serialization support.
216     *
217     * @param stream  the output stream.
218     *
219     * @throws IOException  if there is an I/O error.
220     */
221    private void writeObject(ObjectOutputStream stream) throws IOException {
222        stream.defaultWriteObject();
223        SerialUtils.writePaint(this.paint, stream);
224        SerialUtils.writeStroke(this.stroke, stream);
225    }
226
227    /**
228     * Provides serialization support.
229     *
230     * @param stream  the input stream.
231     *
232     * @throws IOException  if there is an I/O error.
233     * @throws ClassNotFoundException  if there is a classpath problem.
234     */
235    private void readObject(ObjectInputStream stream)
236        throws IOException, ClassNotFoundException {
237        stream.defaultReadObject();
238        this.paint = SerialUtils.readPaint(stream);
239        this.stroke = SerialUtils.readStroke(stream);
240    }
241}
242
243