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