001 /* 002 * $Id: PainterIcon.java 3256 2009-02-10 20:09:41Z kschaefe $ 003 * 004 * Copyright 2006 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.Component; 025 import java.awt.Dimension; 026 import java.awt.Graphics; 027 import java.awt.Graphics2D; 028 import javax.swing.Icon; 029 030 public class PainterIcon implements Icon { 031 Dimension size; 032 private Painter painter; 033 public PainterIcon(Dimension size) { 034 this.size = size; 035 } 036 037 public int getIconHeight() { 038 return size.height; 039 } 040 041 public int getIconWidth() { 042 return size.width; 043 } 044 045 046 public void paintIcon(Component c, Graphics g, int x, int y) { 047 if (getPainter() != null && g instanceof Graphics2D) { 048 g = g.create(); 049 050 try { 051 g.translate(x, y); 052 getPainter().paint((Graphics2D) g, c, size.width, size.height); 053 g.translate(-x, -y); 054 } finally { 055 g.dispose(); 056 } 057 } 058 } 059 060 public Painter getPainter() { 061 return painter; 062 } 063 064 public void setPainter(Painter painter) { 065 this.painter = painter; 066 } 067 }