001 /*
002 * $Id: MattePainter.java,v 1.4 2006/05/14 15:55:53 dmouse Exp $
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
022 package org.jdesktop.swingx.painter;
023
024 import java.awt.Color;
025 import java.awt.Graphics2D;
026 import java.awt.Paint;
027 import javax.swing.JComponent;
028
029 /**
030 * A Painter implementation that uses a Paint to fill the entire background
031 * area using that Paint. For example, if I wanted to paint the entire background
032 * in Color.GREEN, I would:
033 * <pre><code>
034 * MattePainter p = new MattePainter(Color.GREEN);
035 * panel.setBackgroundPainter(p);
036 * </code></pre></p>
037 *
038 * <p>Since it accepts a Paint, it is also possible to paint a texture or use other
039 * more exotic Paint implementations. To paint a BufferedImage texture as the
040 * background:
041 * <pre><code>
042 * TexturePaint paint = new TexturePaint(bufferedImage,
043 * new Rectangle2D.Double(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight()));
044 * MattePainter p = new MattePainter(paint);
045 * panel.setBackgroundPainter(p);
046 * </code></pre></p>
047 *
048 * <p>If no paint is specified, then nothing is painted</p>
049 *
050 * @author rbair
051 */
052 public class MattePainter extends AbstractPainter {
053 /**
054 * The paint to use
055 */
056 private Paint paint;
057
058 /**
059 * Creates a new MattePainter with "null" as the paint used
060 */
061 public MattePainter() {
062 }
063
064 /**
065 * Create a new MattePainter that uses the given color. This is only
066 * a convenience constructor since Color is a Paint, and thus the
067 * other constructor is perfectly suited for specify a color as well
068 *
069 * @param color Color to fill with
070 */
071 public MattePainter(Color color) {
072 this((Paint)color);
073 }
074
075 /**
076 * Create a new MattePainter for the given Paint. This can be a GradientPaint
077 * (though not recommended because the gradient will not grow when the
078 * component becomes larger), TexturePaint, Color, or other Paint instance.
079 *
080 * @param paint Paint to fill with
081 */
082 public MattePainter(Paint paint) {
083 super();
084 this.paint = paint;
085 }
086
087 /**
088 * Sets the Paint to use. If null, nothing is painted
089 *
090 * @param p the Paint to use
091 */
092 public void setPaint(Paint p) {
093 Paint old = getPaint();
094 this.paint = p;
095 firePropertyChange("paint", old, getPaint());
096 }
097
098 /**
099 * @return Gets the Paint being used. May be null
100 */
101 public Paint getPaint() {
102 return paint;
103 }
104
105 /**
106 * @inheritDoc
107 */
108 public void paintBackground(Graphics2D g, JComponent component) {
109 Paint p = getPaint();
110 if (p != null) {
111 g.setPaint(p);
112 g.fillRect(0, 0, component.getWidth(), component.getHeight());
113 }
114 }
115 }