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