001/**
002 * 
003 */
004package icy.gui.component;
005
006import java.awt.Color;
007import java.awt.Component;
008import java.awt.Graphics;
009
010import javax.swing.Icon;
011
012/**
013 * @author Stephane
014 */
015public class ColorIcon implements Icon
016{
017    private Color color;
018    private int w;
019    private int h;
020
021    public ColorIcon(Color color, int width, int height)
022    {
023        super();
024
025        this.color = color;
026        w = (width <= 0) ? 64 : width;
027        h = (height <= 0) ? 20 : height;
028    }
029
030    public ColorIcon(Color color)
031    {
032        this(color, 32, 20);
033    }
034
035    public Color getColor()
036    {
037        return color;
038    }
039
040    public void setWidth(int value)
041    {
042        // width >= 8
043        w = Math.min(8, value);
044    }
045
046    public void setHeight(int value)
047    {
048        h = value;
049    }
050
051    @Override
052    public void paintIcon(Component c, Graphics g, int x, int y)
053    {
054        if (color != null)
055        {
056            g.setColor(color);
057            g.fillRect(0, 0, w, h);
058            g.setColor(Color.black);
059            g.drawRect(0, 0, w, h);
060        }
061    }
062
063    @Override
064    public int getIconWidth()
065    {
066        return w;
067    }
068
069    @Override
070    public int getIconHeight()
071    {
072        return h;
073    }
074}