001/* 002 * $Id: CheckerboardPainter.java 4147 2012-02-01 17:13:24Z kschaefe $ 003 * 004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 022package org.jdesktop.swingx.painter; 023 024import java.awt.Color; 025import java.awt.Graphics2D; 026import java.awt.Paint; 027 028import org.jdesktop.beans.JavaBean; 029import org.jdesktop.swingx.util.PaintUtils; 030 031/** 032 * <p>A Painter implementation that paints a checkerboard pattern. The light 033 * and dark colors (Paint instances) are configurable, as are the size of the 034 * squares (squareSize).</p> 035 * 036 * <p>To configure a checkerboard pattern that used a gradient for the dark 037 * tiles and Color.WHITE for the light tiles, you could: 038 * <pre><code> 039 * GradientPaint gp = new GradientPaint( 040 * new Point2D.Double(0, 0), 041 * Color.BLACK, 042 * new Point2D.Double(0, 32), 043 * Color.GRAY); 044 * CheckerboardPainter p = new CheckerboardPainter(); 045 * p.setDarkPaint(gp); 046 * p.setLightPaint(Color.WHITE); 047 * p.setSquareSize(32); 048 * panel.seBackgroundPainter(p); 049 * </code></pre></p> 050 * 051 * <p>Note that in this example, the "32" in the GradientPaint matches the "32" 052 * set for the squareSize. This is necessary because GradientPaints don't 053 * readjust themselves for the size of the square. They are fixed and immutable 054 * at the time of creation.</p> 055 * 056 * @author rbair 057 */ 058@JavaBean 059@SuppressWarnings("nls") 060public class CheckerboardPainter extends AbstractPainter<Object> { 061 private transient Paint checkerPaint; 062 063 private Paint darkPaint = new Color(204, 204, 204); 064 private Paint lightPaint = Color.WHITE; 065 private double squareSize = 8; 066 067 /** 068 * Create a new CheckerboardPainter. By default the light color is Color.WHITE, 069 * the dark color is a light gray, and the square length is 8. 070 */ 071 public CheckerboardPainter() { 072 } 073 074 /** 075 * Create a new CheckerboardPainter with the specified light and dark paints. 076 * By default the square length is 8. 077 * 078 * @param darkPaint the paint used to draw the dark squares 079 * @param lightPaint the paint used to draw the light squares 080 */ 081 public CheckerboardPainter(Paint darkPaint, Paint lightPaint) { 082 this(darkPaint, lightPaint, 8); 083 } 084 085 /** 086 * Create a new CheckerboardPainter with the specified light and dark paints 087 * and the specified square size. 088 * 089 * @param darkPaint the paint used to draw the dark squares 090 * @param lightPaint the paint used to draw the light squares 091 * @param squareSize the squareSize of the checker board squares 092 */ 093 //TODO squareSize should become int? only ever treated as one 094 public CheckerboardPainter(Paint darkPaint, Paint lightPaint, double squareSize) { 095 this.darkPaint = darkPaint; 096 this.lightPaint = lightPaint; 097 this.squareSize = squareSize; 098 } 099 100 101 /** 102 * Specifies the squareSize of the squares. By default, it is 8. A squareSize of <= 103 * 0 will cause an IllegalArgumentException to be thrown. 104 * 105 * @param squareSize the squareSize of one side of a square tile. Must be > 0. 106 */ 107 public void setSquareSize(double squareSize) { 108 if (squareSize <= 0) { 109 throw new IllegalArgumentException("Length must be > 0"); 110 } 111 112 double old = getSquareSize(); 113 this.squareSize = squareSize; 114 checkerPaint = null; 115 setDirty(true); 116 firePropertyChange("squareSize", old, getSquareSize()); 117 } 118 119 /** 120 * Gets the current square length. 121 * 122 * @return the squareSize. Will be > 0 123 */ 124 public double getSquareSize() { 125 return squareSize; 126 } 127 128 /** 129 * Specifies the paint to use for dark tiles. This is a Paint and 130 * may be anything, including a TexturePaint for painting images. If null, 131 * the background color of the component is used. 132 * 133 * @param color the Paint to use for painting the "dark" tiles. May be null. 134 */ 135 public void setDarkPaint(Paint color) { 136 Paint old = getDarkPaint(); 137 this.darkPaint = color; 138 checkerPaint = null; 139 setDirty(true); 140 firePropertyChange("darkPaint", old, getDarkPaint()); 141 } 142 143 /** 144 * 145 * Gets the current dark paint. 146 * @return the Paint used for painting the "dark" tiles. May be null 147 */ 148 public Paint getDarkPaint() { 149 return darkPaint; 150 } 151 152 /** 153 * Specifies the paint to use for light tiles. This is a Paint and 154 * may be anything, including a TexturePaint for painting images. If null, 155 * the foreground color of the component is used. 156 * 157 * @param color the Paint to use for painting the "light" tiles. May be null. 158 */ 159 public void setLightPaint(Paint color) { 160 Paint old = getLightPaint(); 161 this.lightPaint = color; 162 checkerPaint = null; 163 setDirty(true); 164 firePropertyChange("lightPaint", old, getLightPaint()); 165 } 166 167 /** 168 * 169 * gets the current light paint 170 * @return the Paint used for painting the "light" tiles. May be null 171 */ 172 public Paint getLightPaint() { 173 return lightPaint; 174 } 175 176 /** 177 * Helper method that creates and returns the Paint that incorporates the 178 * sizes and light and dark Paints in one TexturePaint. I may want to cache 179 * this value in the future for performance reasons 180 */ 181 private Paint getCheckerPaint(Object c) { 182 if (checkerPaint == null) { 183 Paint p1 = PainterUtils.getForegroundPaint(getLightPaint(), c); 184 Paint p2 = PainterUtils.getBackgroundPaint(getDarkPaint(), c); 185 186 checkerPaint = PaintUtils.getCheckerPaint(p1, p2, (int)(getSquareSize() * 2)); 187 } 188 return checkerPaint; 189 } 190 191 /** 192 * {@inheritDoc} 193 */ 194 @Override 195 protected void doPaint(Graphics2D g, Object t, int width, int height) { 196 g.setPaint(getCheckerPaint(t)); 197 g.fillRect(0, 0, width, height); 198 } 199}