KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > block > BlockBorder


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------
28  * BlockBorder.java
29  * ----------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: BlockBorder.java,v 1.7.2.1 2005/10/25 20:39:38 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 22-Oct-2004 : Version 1 (DG);
40  * 04-Feb-2005 : Added equals() and implemented Serializable (DG);
41  * 23-Feb-2005 : Added attribute for border color (DG);
42  * 06-May-2005 : Added new convenience constructors (DG);
43  *
44  */

45
46 package org.jfree.chart.block;
47
48 import java.awt.Color JavaDoc;
49 import java.awt.Graphics2D JavaDoc;
50 import java.awt.Paint JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55 import java.io.Serializable JavaDoc;
56
57 import org.jfree.io.SerialUtilities;
58 import org.jfree.ui.RectangleInsets;
59 import org.jfree.util.PaintUtilities;
60
61 /**
62  * A border for a block. This class is immutable.
63  */

64 public class BlockBorder implements Serializable JavaDoc {
65     
66     /** For serialization. */
67     private static final long serialVersionUID = 4961579220410228283L;
68     
69     /** An empty border. */
70     public static final BlockBorder NONE
71         = new BlockBorder(RectangleInsets.ZERO_INSETS, Color.white);
72     
73     /** The space reserved for the border. */
74     private RectangleInsets insets;
75     
76     /** The border color. */
77     private transient Paint JavaDoc paint;
78     
79     /**
80      * Creates a default border.
81      */

82     public BlockBorder() {
83         this(Color.black);
84     }
85     
86     /**
87      * Creates a new border with the specified color.
88      *
89      * @param paint the color (<code>null</code> not permitted).
90      */

91     public BlockBorder(Paint JavaDoc paint) {
92         this(new RectangleInsets(1, 1, 1, 1), paint);
93     }
94     
95     /**
96      * Creates a new border with the specified line widths (in black).
97      *
98      * @param top the width of the top border.
99      * @param left the width of the left border.
100      * @param bottom the width of the bottom border.
101      * @param right the width of the right border.
102      */

103     public BlockBorder(double top, double left, double bottom, double right) {
104         this(new RectangleInsets(top, left, bottom, right), Color.black);
105     }
106     
107     /**
108      * Creates a new border with the specified line widths (in black).
109      *
110      * @param top the width of the top border.
111      * @param left the width of the left border.
112      * @param bottom the width of the bottom border.
113      * @param right the width of the right border.
114      * @param paint the border paint (<code>null</code> not permitted).
115      */

116     public BlockBorder(double top, double left, double bottom, double right,
117                        Paint JavaDoc paint) {
118         this(new RectangleInsets(top, left, bottom, right), paint);
119     }
120     
121     /**
122      * Creates a new border.
123      *
124      * @param insets the border insets (<code>null</code> not permitted).
125      * @param paint the paint (<code>null</code> not permitted).
126      */

127     public BlockBorder(RectangleInsets insets, Paint JavaDoc paint) {
128         if (insets == null) {
129             throw new IllegalArgumentException JavaDoc("Null 'insets' argument.");
130         }
131         if (paint == null) {
132             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
133         }
134         this.insets = insets;
135         this.paint = paint;
136     }
137
138     /**
139      * Returns the space reserved for the border.
140      *
141      * @return The space (never <code>null</code>).
142      */

143     public RectangleInsets getInsets() {
144         return this.insets;
145     }
146     
147     /**
148      * Returns the paint used to draw the border.
149      *
150      * @return The paint (never <code>null</code>).
151      */

152     public Paint JavaDoc getPaint() {
153         return this.paint;
154     }
155     
156     /**
157      * Draws the border by filling in the reserved space (in black).
158      *
159      * @param g2 the graphics device.
160      * @param area the area.
161      */

162     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc area) {
163         // this default implementation will just fill the available
164
// border space with a single color
165
double t = this.insets.calculateTopInset(area.getHeight());
166         double b = this.insets.calculateBottomInset(area.getHeight());
167         double l = this.insets.calculateLeftInset(area.getWidth());
168         double r = this.insets.calculateRightInset(area.getWidth());
169         double x = area.getX();
170         double y = area.getY();
171         double w = area.getWidth();
172         double h = area.getHeight();
173         g2.setPaint(this.paint);
174         Rectangle2D JavaDoc rect = new Rectangle2D.Double JavaDoc();
175         if (t > 0.0) {
176             rect.setRect(x, y, w, t);
177             g2.fill(rect);
178         }
179         if (b > 0.0) {
180             rect.setRect(x, y + h - b, w, b);
181             g2.fill(rect);
182         }
183         if (l > 0.0) {
184             rect.setRect(x, y, l, h);
185             g2.fill(rect);
186         }
187         if (r > 0.0) {
188             rect.setRect(x + w - r, y, r, h);
189             g2.fill(rect);
190         }
191     }
192     
193     /**
194      * Tests this border for equality with an arbitrary instance.
195      *
196      * @param obj the object (<code>null</code> permitted).
197      *
198      * @return A boolean.
199      */

200     public boolean equals(Object JavaDoc obj) {
201         if (obj == this) {
202             return true;
203         }
204         if (!(obj instanceof BlockBorder)) {
205             return false;
206         }
207         BlockBorder that = (BlockBorder) obj;
208         if (!this.insets.equals(that.insets)) {
209             return false;
210         }
211         if (!PaintUtilities.equal(this.paint, that.paint)) {
212             return false;
213         }
214         return true;
215     }
216     
217     /**
218      * Provides serialization support.
219      *
220      * @param stream the output stream.
221      *
222      * @throws IOException if there is an I/O error.
223      */

224     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
225         stream.defaultWriteObject();
226         SerialUtilities.writePaint(this.paint, stream);
227     }
228
229     /**
230      * Provides serialization support.
231      *
232      * @param stream the input stream.
233      *
234      * @throws IOException if there is an I/O error.
235      * @throws ClassNotFoundException if there is a classpath problem.
236      */

237     private void readObject(ObjectInputStream JavaDoc stream)
238         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
239         stream.defaultReadObject();
240         this.paint = SerialUtilities.readPaint(stream);
241     }
242  
243 }
244
Popular Tags