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 * ColorBlock.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.Graphics2D; 040import java.awt.Paint; 041import java.awt.geom.Rectangle2D; 042import java.io.IOException; 043import java.io.ObjectInputStream; 044import java.io.ObjectOutputStream; 045import org.jfree.chart.HashUtils; 046import org.jfree.chart.ui.Size2D; 047import org.jfree.chart.util.PaintUtils; 048import org.jfree.chart.util.Args; 049import org.jfree.chart.util.SerialUtils; 050 051/** 052 * A block that is filled with a single color. 053 */ 054public class ColorBlock extends AbstractBlock implements Block { 055 056 /** For serialization. */ 057 static final long serialVersionUID = 3383866145634010865L; 058 059 /** The paint. */ 060 private transient Paint paint; 061 062 /** 063 * Creates a new block. 064 * 065 * @param paint the paint ({@code null} not permitted). 066 * @param width the width. 067 * @param height the height. 068 */ 069 public ColorBlock(Paint paint, double width, double height) { 070 Args.nullNotPermitted(paint, "paint"); 071 this.paint = paint; 072 setWidth(width); 073 setHeight(height); 074 } 075 076 /** 077 * Returns the paint. 078 * 079 * @return The paint (never {@code null}). 080 */ 081 public Paint getPaint() { 082 return this.paint; 083 } 084 085 /** 086 * Arranges the contents of the block, within the given constraints, and 087 * returns the block size. 088 * 089 * @param g2 the graphics device. 090 * @param constraint the constraint ({@code null} not permitted). 091 * 092 * @return The block size (in Java2D units, never {@code null}). 093 */ 094 @Override 095 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 096 return new Size2D(calculateTotalWidth(getWidth()), 097 calculateTotalHeight(getHeight())); 098 } 099 100 /** 101 * Draws the block. 102 * 103 * @param g2 the graphics device. 104 * @param area the area. 105 */ 106 @Override 107 public void draw(Graphics2D g2, Rectangle2D area) { 108 area = trimMargin(area); 109 drawBorder(g2, area); 110 area = trimBorder(area); 111 area = trimPadding(area); 112 g2.setPaint(this.paint); 113 g2.fill(area); 114 } 115 116 /** 117 * Draws the block within the specified area. 118 * 119 * @param g2 the graphics device. 120 * @param area the area. 121 * @param params ignored ({@code null} permitted). 122 * 123 * @return Always {@code null}. 124 */ 125 @Override 126 public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 127 draw(g2, area); 128 return null; 129 } 130 131 /** 132 * Tests this block for equality with an arbitrary object. 133 * 134 * @param obj the object ({@code null} permitted). 135 * 136 * @return A boolean. 137 */ 138 @Override 139 public boolean equals(Object obj) { 140 if (obj == this) { 141 return true; 142 } 143 if (!(obj instanceof ColorBlock)) { 144 return false; 145 } 146 ColorBlock that = (ColorBlock) obj; 147 if (!PaintUtils.equal(this.paint, that.paint)) { 148 return false; 149 } 150 return super.equals(obj); 151 } 152 153 @Override 154 public int hashCode() { 155 int hash = super.hashCode(); 156 hash = 79 * hash + HashUtils.hashCodeForPaint(this.paint); 157 return hash; 158 } 159 160 /** 161 * Provides serialization support. 162 * 163 * @param stream the output stream. 164 * 165 * @throws IOException if there is an I/O error. 166 */ 167 private void writeObject(ObjectOutputStream stream) throws IOException { 168 stream.defaultWriteObject(); 169 SerialUtils.writePaint(this.paint, stream); 170 } 171 172 /** 173 * Provides serialization support. 174 * 175 * @param stream the input stream. 176 * 177 * @throws IOException if there is an I/O error. 178 * @throws ClassNotFoundException if there is a classpath problem. 179 */ 180 private void readObject(ObjectInputStream stream) 181 throws IOException, ClassNotFoundException { 182 stream.defaultReadObject(); 183 this.paint = SerialUtils.readPaint(stream); 184 } 185 186}