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