001 /* 002 * $Id: IconPainter.java,v 1.2 2006/03/24 11:22:16 gfx 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.Graphics2D; 025 import java.awt.Image; 026 import java.awt.image.BufferedImage; 027 import javax.swing.Icon; 028 import javax.swing.ImageIcon; 029 import javax.swing.JComponent; 030 031 /** 032 * <p>An ImagePainter subclass that provides convenience methods "setIcon" and 033 * "getIcon" to use Icons (and ImageIcons) rather than Images directly. Because 034 * this class uses "Icon" and because there is a "getImage" method from the 035 * parent class, sometimes an intermediate BufferedImage will be created to 036 * generate the image to return. This should be fairly performant, but will 037 * of course have some overhead. To avoid this overhead, use an ImageIcon 038 * where possible.</p> 039 * 040 * @author rbair 041 */ 042 public class IconPainter extends ImagePainter { 043 /** 044 * The icon to draw 045 */ 046 private Icon icon; 047 048 /** 049 * Create a new IconPainter 050 */ 051 public IconPainter() { 052 super(); 053 } 054 055 /** 056 * Create a new IconPainter with the specified icon. 057 * 058 * @param icon the icon to be painted 059 */ 060 public IconPainter(Icon icon) { 061 super(); 062 this.icon = icon; 063 } 064 065 066 /** 067 * Set the icon to use. This will fire property change notification not 068 * only for the "icon" property, but also for the "image" property. 069 * 070 * @param icon the Icon to use 071 */ 072 public void setIcon(Icon icon) { 073 Icon old = getIcon(); 074 Image oldImage = getImage(); 075 this.icon = icon; 076 firePropertyChange("icon", old, getIcon()); 077 firePropertyChange("image", oldImage, getImage()); 078 } 079 080 /** 081 * @return the Icon used by this painter 082 */ 083 public Icon getIcon() { 084 return icon; 085 } 086 087 /** 088 * @return the image used for painting the background of this panel 089 */ 090 public Image getImage() { 091 if (icon instanceof ImageIcon) { 092 return ((ImageIcon)icon).getImage(); 093 } else if (icon == null) { 094 return null; 095 } else { 096 //paint the icon into a buffered image 097 BufferedImage image = new BufferedImage( 098 icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 099 Graphics2D g2 = image.createGraphics(); 100 icon.paintIcon(new JComponent(){}, g2, 0, 0); 101 g2.dispose(); 102 return image; 103 } 104 } 105 }