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