001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2017, by Object Refinery Limited 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-2016, by Christo Zietsman and Contributors.
031 *
032 * Original Author:  Christo Zietsman;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 */
036
037package org.jfree.chart.block;
038
039import java.awt.BasicStroke;
040import java.awt.Color;
041import java.awt.Graphics2D;
042import java.awt.Paint;
043import java.awt.RenderingHints;
044import java.awt.Stroke;
045import java.awt.geom.Line2D;
046import java.awt.geom.Rectangle2D;
047import java.io.IOException;
048import java.io.ObjectInputStream;
049import java.io.ObjectOutputStream;
050import java.io.Serializable;
051import org.jfree.chart.ui.RectangleInsets;
052import org.jfree.chart.util.ObjectUtils;
053import org.jfree.chart.util.PaintUtils;
054import org.jfree.chart.util.Args;
055import org.jfree.chart.util.SerialUtils;
056
057/**
058 * A line border for any {@link AbstractBlock}.
059 *
060 * @since 1.0.5
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 (!ObjectUtils.equal(this.stroke, that.stroke)) {
197            return false;
198        }
199        if (!this.insets.equals(that.insets)) {
200            return false;
201        }
202        return true;
203    }
204
205    /**
206     * Provides serialization support.
207     *
208     * @param stream  the output stream.
209     *
210     * @throws IOException  if there is an I/O error.
211     */
212    private void writeObject(ObjectOutputStream stream) throws IOException {
213        stream.defaultWriteObject();
214        SerialUtils.writePaint(this.paint, stream);
215        SerialUtils.writeStroke(this.stroke, stream);
216    }
217
218    /**
219     * Provides serialization support.
220     *
221     * @param stream  the input stream.
222     *
223     * @throws IOException  if there is an I/O error.
224     * @throws ClassNotFoundException  if there is a classpath problem.
225     */
226    private void readObject(ObjectInputStream stream)
227        throws IOException, ClassNotFoundException {
228        stream.defaultReadObject();
229        this.paint = SerialUtils.readPaint(stream);
230        this.stroke = SerialUtils.readStroke(stream);
231    }
232}
233
234