001/*
002 * $Id: PainterIcon.java 3927 2011-02-22 16:34:11Z kleopatra $
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
022package org.jdesktop.swingx.icon;
023
024import java.awt.Component;
025import java.awt.Dimension;
026import java.awt.Graphics;
027import java.awt.Graphics2D;
028
029import javax.swing.Icon;
030
031import org.jdesktop.swingx.painter.Painter;
032
033public class PainterIcon implements Icon {
034    Dimension size;
035    private Painter painter;
036    public PainterIcon(Dimension size) {
037        this.size = size;
038    }
039    
040    @Override
041    public int getIconHeight() {
042        return size.height;
043    }
044    
045    @Override
046    public int getIconWidth() {
047        return size.width;
048    }
049    
050    
051    @Override
052    public void paintIcon(Component c, Graphics g, int x, int y) {
053        if (getPainter() != null && g instanceof Graphics2D) {
054            g = g.create();
055            
056            try {
057                g.translate(x, y);
058                getPainter().paint((Graphics2D) g, c, size.width, size.height);
059                g.translate(-x, -y);
060            } finally {
061                g.dispose();
062            }
063        }
064    }
065
066    public Painter getPainter() {
067        return painter;
068    }
069
070    public void setPainter(Painter painter) {
071        this.painter = painter;
072    }
073}