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