Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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: * LineBorder.java 29: * --------------- 30: * (C) Copyright 2007, by Christo Zietsman and Contributors. 31: * 32: * Original Author: Christo Zietsman; 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * 35: * Changes: 36: * -------- 37: * 16-Mar-2007 : Version 1, contributed by Christo Zietsman with 38: * modifications by DG (DG); 39: * 13-Jun-2007 : Don't draw if area doesn't have positive dimensions (DG); 40: * 41: */ 42: 43: package org.jfree.chart.block; 44: 45: import java.awt.BasicStroke; 46: import java.awt.Color; 47: import java.awt.Graphics2D; 48: import java.awt.Paint; 49: import java.awt.Stroke; 50: import java.awt.geom.Line2D; 51: import java.awt.geom.Rectangle2D; 52: import java.io.IOException; 53: import java.io.ObjectInputStream; 54: import java.io.ObjectOutputStream; 55: import java.io.Serializable; 56: 57: import org.jfree.io.SerialUtilities; 58: import org.jfree.ui.RectangleInsets; 59: import org.jfree.util.ObjectUtilities; 60: import org.jfree.util.PaintUtilities; 61: 62: /** 63: * A line border for any {@link AbstractBlock}. 64: * 65: * @since 1.0.5 66: */ 67: public class LineBorder implements BlockFrame, Serializable { 68: 69: /** For serialization. */ 70: static final long serialVersionUID = 4630356736707233924L; 71: 72: /** The line color. */ 73: private transient Paint paint; 74: 75: /** The line stroke. */ 76: private transient Stroke stroke; 77: 78: /** The insets. */ 79: private RectangleInsets insets; 80: 81: /** 82: * Creates a default border. 83: */ 84: public LineBorder() { 85: this(Color.black, new BasicStroke(1.0f), new RectangleInsets(1.0, 1.0, 86: 1.0, 1.0)); 87: } 88: 89: /** 90: * Creates a new border with the specified color. 91: * 92: * @param paint the color (<code>null</code> not permitted). 93: * @param stroke the border stroke (<code>null</code> not permitted). 94: * @param insets the insets (<code>null</code> not permitted). 95: */ 96: public LineBorder(Paint paint, Stroke stroke, RectangleInsets insets) { 97: if (paint == null) { 98: throw new IllegalArgumentException("Null 'paint' argument."); 99: } 100: if (stroke == null) { 101: throw new IllegalArgumentException("Null 'stroke' argument."); 102: } 103: if (insets == null) { 104: throw new IllegalArgumentException("Null 'insets' argument."); 105: } 106: this.paint = paint; 107: this.stroke = stroke; 108: this.insets = insets; 109: } 110: 111: /** 112: * Returns the paint. 113: * 114: * @return The paint (never <code>null</code>). 115: */ 116: public Paint getPaint() { 117: return this.paint; 118: } 119: 120: /** 121: * Returns the insets. 122: * 123: * @return The insets (never <code>null</code>). 124: */ 125: public RectangleInsets getInsets() { 126: return this.insets; 127: } 128: 129: /** 130: * Returns the stroke. 131: * 132: * @return The stroke (never <code>null</code>). 133: */ 134: public Stroke getStroke() { 135: return this.stroke; 136: } 137: 138: /** 139: * Draws the border by filling in the reserved space (in black). 140: * 141: * @param g2 the graphics device. 142: * @param area the area. 143: */ 144: public void draw(Graphics2D g2, Rectangle2D area) { 145: double w = area.getWidth(); 146: double h = area.getHeight(); 147: // if the area has zero height or width, we shouldn't draw anything 148: if (w <= 0.0 || h <= 0.0) { 149: return; 150: } 151: double t = this.insets.calculateTopInset(h); 152: double b = this.insets.calculateBottomInset(h); 153: double l = this.insets.calculateLeftInset(w); 154: double r = this.insets.calculateRightInset(w); 155: double x = area.getX(); 156: double y = area.getY(); 157: double x0 = x + l / 2.0; 158: double x1 = x + w - r / 2.0; 159: double y0 = y + h - b / 2.0; 160: double y1 = y + t / 2.0; 161: g2.setPaint(getPaint()); 162: g2.setStroke(getStroke()); 163: Line2D line = new Line2D.Double(); 164: if (t > 0.0) { 165: line.setLine(x0, y1, x1, y1); 166: g2.draw(line); 167: } 168: if (b > 0.0) { 169: line.setLine(x0, y0, x1, y0); 170: g2.draw(line); 171: } 172: if (l > 0.0) { 173: line.setLine(x0, y0, x0, y1); 174: g2.draw(line); 175: } 176: if (r > 0.0) { 177: line.setLine(x1, y0, x1, y1); 178: g2.draw(line); 179: } 180: } 181: 182: /** 183: * Tests this border for equality with an arbitrary instance. 184: * 185: * @param obj the object (<code>null</code> permitted). 186: * 187: * @return A boolean. 188: */ 189: public boolean equals(Object obj) { 190: if (obj == this) { 191: return true; 192: } 193: if (!(obj instanceof LineBorder)) { 194: return false; 195: } 196: LineBorder that = (LineBorder) obj; 197: if (!PaintUtilities.equal(this.paint, that.paint)) { 198: return false; 199: } 200: if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 201: return false; 202: } 203: if (!this.insets.equals(that.insets)) { 204: return false; 205: } 206: return true; 207: } 208: 209: /** 210: * Provides serialization support. 211: * 212: * @param stream the output stream. 213: * 214: * @throws IOException if there is an I/O error. 215: */ 216: private void writeObject(ObjectOutputStream stream) throws IOException { 217: stream.defaultWriteObject(); 218: SerialUtilities.writePaint(this.paint, stream); 219: SerialUtilities.writeStroke(this.stroke, stream); 220: } 221: 222: /** 223: * Provides serialization support. 224: * 225: * @param stream the input stream. 226: * 227: * @throws IOException if there is an I/O error. 228: * @throws ClassNotFoundException if there is a classpath problem. 229: */ 230: private void readObject(ObjectInputStream stream) 231: throws IOException, ClassNotFoundException { 232: stream.defaultReadObject(); 233: this.paint = SerialUtilities.readPaint(stream); 234: this.stroke = SerialUtilities.readStroke(stream); 235: } 236: } 237: 238: