001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------
028: * LineBorder.java
029: * ---------------
030: * (C) Copyright 2007, by Christo Zietsman and Contributors.
031: *
032: * Original Author: Christo Zietsman;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: LineBorder.java,v 1.1.2.2 2007/06/14 09:16:42 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 16-Mar-2007 : Version 1, contributed by Christo Zietsman with
040: * modifications by DG (DG);
041: * 13-Jun-2007 : Don't draw if area doesn't have positive dimensions (DG);
042: *
043: */
044:
045: package org.jfree.chart.block;
046:
047: import java.awt.BasicStroke;
048: import java.awt.Color;
049: import java.awt.Graphics2D;
050: import java.awt.Paint;
051: import java.awt.Stroke;
052: import java.awt.geom.Line2D;
053: import java.awt.geom.Rectangle2D;
054: import java.io.IOException;
055: import java.io.ObjectInputStream;
056: import java.io.ObjectOutputStream;
057: import java.io.Serializable;
058:
059: import org.jfree.io.SerialUtilities;
060: import org.jfree.ui.RectangleInsets;
061: import org.jfree.util.ObjectUtilities;
062: import org.jfree.util.PaintUtilities;
063:
064: /**
065: * A line border for any {@link AbstractBlock}.
066: *
067: * @since 1.0.5
068: */
069: public class LineBorder implements BlockFrame, Serializable {
070:
071: /** The line color. */
072: private transient Paint paint;
073:
074: /** The line stroke. */
075: private transient Stroke stroke;
076:
077: /** The insets. */
078: private RectangleInsets insets;
079:
080: /**
081: * Creates a default border.
082: */
083: public LineBorder() {
084: this (Color.black, new BasicStroke(1.0f), new RectangleInsets(
085: 1.0, 1.0, 1.0, 1.0));
086: }
087:
088: /**
089: * Creates a new border with the specified color.
090: *
091: * @param paint the color (<code>null</code> not permitted).
092: * @param stroke the border stroke (<code>null</code> not permitted).
093: * @param insets the insets (<code>null</code> not permitted).
094: */
095: public LineBorder(Paint paint, Stroke stroke, RectangleInsets insets) {
096: if (paint == null) {
097: throw new IllegalArgumentException("Null 'paint' argument.");
098: }
099: if (stroke == null) {
100: throw new IllegalArgumentException(
101: "Null 'stroke' argument.");
102: }
103: if (insets == null) {
104: throw new IllegalArgumentException(
105: "Null 'insets' argument.");
106: }
107: this .paint = paint;
108: this .stroke = stroke;
109: this .insets = insets;
110: }
111:
112: /**
113: * Returns the paint.
114: *
115: * @return The paint (never <code>null</code>).
116: */
117: public Paint getPaint() {
118: return this .paint;
119: }
120:
121: /**
122: * Returns the insets.
123: *
124: * @return The insets (never <code>null</code>).
125: */
126: public RectangleInsets getInsets() {
127: return this .insets;
128: }
129:
130: /**
131: * Returns the stroke.
132: *
133: * @return The stroke (never <code>null</code>).
134: */
135: public Stroke getStroke() {
136: return this .stroke;
137: }
138:
139: /**
140: * Draws the border by filling in the reserved space (in black).
141: *
142: * @param g2 the graphics device.
143: * @param area the area.
144: */
145: public void draw(Graphics2D g2, Rectangle2D area) {
146: double w = area.getWidth();
147: double h = area.getHeight();
148: // if the area has zero height or width, we shouldn't draw anything
149: if (w <= 0.0 || h <= 0.0) {
150: return;
151: }
152: double t = this .insets.calculateTopInset(h);
153: double b = this .insets.calculateBottomInset(h);
154: double l = this .insets.calculateLeftInset(w);
155: double r = this .insets.calculateRightInset(w);
156: double x = area.getX();
157: double y = area.getY();
158: double x0 = x + l / 2.0;
159: double x1 = x + w - r / 2.0;
160: double y0 = y + h - b / 2.0;
161: double y1 = y + t / 2.0;
162: g2.setPaint(getPaint());
163: g2.setStroke(getStroke());
164: Line2D line = new Line2D.Double();
165: if (t > 0.0) {
166: line.setLine(x0, y1, x1, y1);
167: g2.draw(line);
168: }
169: if (b > 0.0) {
170: line.setLine(x0, y0, x1, y0);
171: g2.draw(line);
172: }
173: if (l > 0.0) {
174: line.setLine(x0, y0, x0, y1);
175: g2.draw(line);
176: }
177: if (r > 0.0) {
178: line.setLine(x1, y0, x1, y1);
179: g2.draw(line);
180: }
181: }
182:
183: /**
184: * Tests this border for equality with an arbitrary instance.
185: *
186: * @param obj the object (<code>null</code> permitted).
187: *
188: * @return A boolean.
189: */
190: public boolean equals(Object obj) {
191: if (obj == this ) {
192: return true;
193: }
194: if (!(obj instanceof LineBorder)) {
195: return false;
196: }
197: LineBorder that = (LineBorder) obj;
198: if (!PaintUtilities.equal(this .paint, that.paint)) {
199: return false;
200: }
201: if (!ObjectUtilities.equal(this .stroke, that.stroke)) {
202: return false;
203: }
204: if (!this .insets.equals(that.insets)) {
205: return false;
206: }
207: return true;
208: }
209:
210: /**
211: * Provides serialization support.
212: *
213: * @param stream the output stream.
214: *
215: * @throws IOException if there is an I/O error.
216: */
217: private void writeObject(ObjectOutputStream stream)
218: throws IOException {
219: stream.defaultWriteObject();
220: SerialUtilities.writePaint(this .paint, stream);
221: SerialUtilities.writeStroke(this .stroke, stream);
222: }
223:
224: /**
225: * Provides serialization support.
226: *
227: * @param stream the input stream.
228: *
229: * @throws IOException if there is an I/O error.
230: * @throws ClassNotFoundException if there is a classpath problem.
231: */
232: private void readObject(ObjectInputStream stream)
233: throws IOException, ClassNotFoundException {
234: stream.defaultReadObject();
235: this.paint = SerialUtilities.readPaint(stream);
236: this.stroke = SerialUtilities.readStroke(stream);
237: }
238: }
|