001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, 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-2014, 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.util.ParamChecks; 060 061import org.jfree.io.SerialUtilities; 062import org.jfree.ui.RectangleInsets; 063import org.jfree.util.ObjectUtilities; 064import org.jfree.util.PaintUtilities; 065 066/** 067 * A line border for any {@link AbstractBlock}. 068 * 069 * @since 1.0.5 070 */ 071public class LineBorder implements BlockFrame, Serializable { 072 073 /** For serialization. */ 074 static final long serialVersionUID = 4630356736707233924L; 075 076 /** The line color. */ 077 private transient Paint paint; 078 079 /** The line stroke. */ 080 private transient Stroke stroke; 081 082 /** The insets. */ 083 private RectangleInsets insets; 084 085 /** 086 * Creates a default border. 087 */ 088 public LineBorder() { 089 this(Color.black, new BasicStroke(1.0f), new RectangleInsets(1.0, 1.0, 090 1.0, 1.0)); 091 } 092 093 /** 094 * Creates a new border with the specified color. 095 * 096 * @param paint the color ({@code null} not permitted). 097 * @param stroke the border stroke ({@code null} not permitted). 098 * @param insets the insets ({@code null} not permitted). 099 */ 100 public LineBorder(Paint paint, Stroke stroke, RectangleInsets insets) { 101 ParamChecks.nullNotPermitted(paint, "paint"); 102 ParamChecks.nullNotPermitted(stroke, "stroke"); 103 ParamChecks.nullNotPermitted(insets, "insets"); 104 this.paint = paint; 105 this.stroke = stroke; 106 this.insets = insets; 107 } 108 109 /** 110 * Returns the paint. 111 * 112 * @return The paint (never {@code null}). 113 */ 114 public Paint getPaint() { 115 return this.paint; 116 } 117 118 /** 119 * Returns the insets. 120 * 121 * @return The insets (never {@code null}). 122 */ 123 @Override 124 public RectangleInsets getInsets() { 125 return this.insets; 126 } 127 128 /** 129 * Returns the stroke. 130 * 131 * @return The stroke (never {@code null}). 132 */ 133 public Stroke getStroke() { 134 return this.stroke; 135 } 136 137 /** 138 * Draws the border by filling in the reserved space (in black). 139 * 140 * @param g2 the graphics device. 141 * @param area the area. 142 */ 143 @Override 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 Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL); 164 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 165 RenderingHints.VALUE_STROKE_NORMALIZE); 166 Line2D line = new Line2D.Double(); 167 if (t > 0.0) { 168 line.setLine(x0, y1, x1, y1); 169 g2.draw(line); 170 } 171 if (b > 0.0) { 172 line.setLine(x0, y0, x1, y0); 173 g2.draw(line); 174 } 175 if (l > 0.0) { 176 line.setLine(x0, y0, x0, y1); 177 g2.draw(line); 178 } 179 if (r > 0.0) { 180 line.setLine(x1, y0, x1, y1); 181 g2.draw(line); 182 } 183 g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved); 184 } 185 186 /** 187 * Tests this border for equality with an arbitrary instance. 188 * 189 * @param obj the object ({@code null} permitted). 190 * 191 * @return A boolean. 192 */ 193 @Override 194 public boolean equals(Object obj) { 195 if (obj == this) { 196 return true; 197 } 198 if (!(obj instanceof LineBorder)) { 199 return false; 200 } 201 LineBorder that = (LineBorder) obj; 202 if (!PaintUtilities.equal(this.paint, that.paint)) { 203 return false; 204 } 205 if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 206 return false; 207 } 208 if (!this.insets.equals(that.insets)) { 209 return false; 210 } 211 return true; 212 } 213 214 /** 215 * Provides serialization support. 216 * 217 * @param stream the output stream. 218 * 219 * @throws IOException if there is an I/O error. 220 */ 221 private void writeObject(ObjectOutputStream stream) throws IOException { 222 stream.defaultWriteObject(); 223 SerialUtilities.writePaint(this.paint, stream); 224 SerialUtilities.writeStroke(this.stroke, stream); 225 } 226 227 /** 228 * Provides serialization support. 229 * 230 * @param stream the input stream. 231 * 232 * @throws IOException if there is an I/O error. 233 * @throws ClassNotFoundException if there is a classpath problem. 234 */ 235 private void readObject(ObjectInputStream stream) 236 throws IOException, ClassNotFoundException { 237 stream.defaultReadObject(); 238 this.paint = SerialUtilities.readPaint(stream); 239 this.stroke = SerialUtilities.readStroke(stream); 240 } 241} 242 243