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