001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert 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 * BlockBorder.java 029 * ---------------- 030 * (C) Copyright 2004-present, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier); 034 * 035 */ 036 037package org.jfree.chart.block; 038 039import java.awt.Color; 040import java.awt.Graphics2D; 041import java.awt.Paint; 042import java.awt.geom.Rectangle2D; 043import java.io.IOException; 044import java.io.ObjectInputStream; 045import java.io.ObjectOutputStream; 046import java.io.Serializable; 047import java.util.Objects; 048import org.jfree.chart.HashUtils; 049import org.jfree.chart.ui.RectangleInsets; 050import org.jfree.chart.util.PaintUtils; 051import org.jfree.chart.util.Args; 052import org.jfree.chart.util.SerialUtils; 053 054/** 055 * A border for a block. This class is immutable. 056 */ 057public class BlockBorder implements BlockFrame, Serializable { 058 059 /** For serialization. */ 060 private static final long serialVersionUID = 4961579220410228283L; 061 062 /** An empty border. */ 063 public static final BlockBorder NONE = new BlockBorder( 064 RectangleInsets.ZERO_INSETS, Color.WHITE); 065 066 /** The space reserved for the border. */ 067 private final RectangleInsets insets; 068 069 /** The border color. */ 070 private transient Paint paint; 071 072 /** 073 * Creates a default border. 074 */ 075 public BlockBorder() { 076 this(Color.BLACK); 077 } 078 079 /** 080 * Creates a new border with the specified color. 081 * 082 * @param paint the color ({@code null} not permitted). 083 */ 084 public BlockBorder(Paint paint) { 085 this(new RectangleInsets(1, 1, 1, 1), paint); 086 } 087 088 /** 089 * Creates a new border with the specified line widths (in black). 090 * 091 * @param top the width of the top border. 092 * @param left the width of the left border. 093 * @param bottom the width of the bottom border. 094 * @param right the width of the right border. 095 */ 096 public BlockBorder(double top, double left, double bottom, double right) { 097 this(new RectangleInsets(top, left, bottom, right), Color.BLACK); 098 } 099 100 /** 101 * Creates a new border with the specified line widths (in black). 102 * 103 * @param top the width of the top border. 104 * @param left the width of the left border. 105 * @param bottom the width of the bottom border. 106 * @param right the width of the right border. 107 * @param paint the border paint ({@code null} not permitted). 108 */ 109 public BlockBorder(double top, double left, double bottom, double right, 110 Paint paint) { 111 this(new RectangleInsets(top, left, bottom, right), paint); 112 } 113 114 /** 115 * Creates a new border. 116 * 117 * @param insets the border insets ({@code null} not permitted). 118 * @param paint the paint ({@code null} not permitted). 119 */ 120 public BlockBorder(RectangleInsets insets, Paint paint) { 121 Args.nullNotPermitted(insets, "insets"); 122 Args.nullNotPermitted(paint, "paint"); 123 this.insets = insets; 124 this.paint = paint; 125 } 126 127 /** 128 * Returns the space reserved for the border. 129 * 130 * @return The space (never {@code null}). 131 */ 132 @Override 133 public RectangleInsets getInsets() { 134 return this.insets; 135 } 136 137 /** 138 * Returns the paint used to draw the border. 139 * 140 * @return The paint (never {@code null}). 141 */ 142 public Paint getPaint() { 143 return this.paint; 144 } 145 146 /** 147 * Draws the border by filling in the reserved space. 148 * 149 * @param g2 the graphics device. 150 * @param area the area. 151 */ 152 @Override 153 public void draw(Graphics2D g2, Rectangle2D area) { 154 // this default implementation will just fill the available 155 // border space with a single color 156 double t = this.insets.calculateTopInset(area.getHeight()); 157 double b = this.insets.calculateBottomInset(area.getHeight()); 158 double l = this.insets.calculateLeftInset(area.getWidth()); 159 double r = this.insets.calculateRightInset(area.getWidth()); 160 double x = area.getX(); 161 double y = area.getY(); 162 double w = area.getWidth(); 163 double h = area.getHeight(); 164 g2.setPaint(this.paint); 165 Rectangle2D rect = new Rectangle2D.Double(); 166 if (t > 0.0) { 167 rect.setRect(x, y, w, t); 168 g2.fill(rect); 169 } 170 if (b > 0.0) { 171 rect.setRect(x, y + h - b, w, b); 172 g2.fill(rect); 173 } 174 if (l > 0.0) { 175 rect.setRect(x, y, l, h); 176 g2.fill(rect); 177 } 178 if (r > 0.0) { 179 rect.setRect(x + w - r, y, r, h); 180 g2.fill(rect); 181 } 182 } 183 184 /** 185 * Tests this border for equality with an arbitrary instance. 186 * 187 * @param obj the object ({@code null} permitted). 188 * 189 * @return A boolean. 190 */ 191 @Override 192 public boolean equals(Object obj) { 193 if (obj == this) { 194 return true; 195 } 196 if (!(obj instanceof BlockBorder)) { 197 return false; 198 } 199 BlockBorder that = (BlockBorder) obj; 200 if (!Objects.equals(this.insets, that.insets)) { 201 return false; 202 } 203 if (!PaintUtils.equal(this.paint, that.paint)) { 204 return false; 205 } 206 return true; 207 } 208 209 @Override 210 public int hashCode() { 211 int hash = 5; 212 hash = 37 * hash + Objects.hashCode(this.insets); 213 hash = 37 * hash + HashUtils.hashCodeForPaint(this.paint); 214 return hash; 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 stream) throws IOException { 225 stream.defaultWriteObject(); 226 SerialUtils.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 stream) 238 throws IOException, ClassNotFoundException { 239 stream.defaultReadObject(); 240 this.paint = SerialUtils.readPaint(stream); 241 } 242 243}