001 /*
002 * $Id: MattePainter.java 3288 2009-03-10 14:36:28Z 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
022 package org.jdesktop.swingx.painter;
023
024 import java.awt.Graphics2D;
025 import java.awt.Paint;
026 import java.awt.Rectangle;
027 import java.awt.Shape;
028
029 /**
030 * A Painter implementation that uses a Paint to fill the entire background
031 * area. For example, if I wanted to paint the entire background of a panel green, I would:
032 * <pre><code>
033 * MattePainter p = new MattePainter(Color.GREEN);
034 * panel.setBackgroundPainter(p);
035 * </code></pre></p>
036 *
037 * <p>Since it accepts a Paint, it is also possible to paint a texture or use other
038 * more exotic Paint implementations. To paint a BufferedImage texture as the
039 * background:
040 * <pre><code>
041 * TexturePaint paint = new TexturePaint(bufferedImage,
042 * new Rectangle2D.Double(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight()));
043 * MattePainter p = new MattePainter(paint);
044 * panel.setBackgroundPainter(p);
045 * </code></pre></p>
046 *
047 * <p>If no paint is specified, then nothing is painted</p>
048 * @author rbair
049 */
050 public class MattePainter extends AbstractAreaPainter<Object> {
051
052 /**
053 * Creates a new MattePainter with "null" as the paint used
054 */
055 public MattePainter() {
056 }
057
058 /**
059 * Create a new MattePainter for the given Paint. This can be a GradientPaint
060 * (the gradient will not grow when the component becomes larger unless
061 * you use the paintStretched boolean property),
062 * TexturePaint, Color, or other Paint instance.
063 *
064 * @param paint Paint to fill with
065 */
066 public MattePainter(Paint paint) {
067 super(paint);
068 }
069
070 /**
071 * Create a new MattePainter for the given Paint. This can be a GradientPaint
072 * (the gradient will not grow when the component becomes larger unless
073 * you use the paintStretched boolean property),
074 * TexturePaint, Color, or other Paint instance.
075 *
076 * @param paint Paint to fill with
077 * @param paintStretched indicates if the paint should be stretched
078 */
079 public MattePainter(Paint paint, boolean paintStretched) {
080 super(paint);
081 this.setPaintStretched(paintStretched);
082 }
083
084 /**
085 * {@inheritDoc}
086 */
087 protected void doPaint(Graphics2D g, Object component, int width, int height) {
088 Paint p = getFillPaint();
089 if (p != null) {
090 if(isPaintStretched()) {
091 p = calculateSnappedPaint(p,width,height);
092 }
093 g.setPaint(p);
094 g.fillRect(0, 0, width, height);
095 }
096 }
097
098 public Shape provideShape(Graphics2D g, Object comp, int width, int height) {
099 return new Rectangle(0,0,width,height);
100 }
101
102 }