All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.valkyriercp.taskpane.DefaultTaskPaneIconGenerator Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package org.valkyriercp.taskpane;

import org.valkyriercp.command.support.AbstractCommand;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class DefaultTaskPaneIconGenerator implements IconGenerator
{
    public static final int ROUND = 0;
    public static final int DIAMOND = 1;
    public static final int SQUARE = 2;
    public static final int OCTAGON = 3;

    private Color iconColor = Color.DARK_GRAY;
    private int iconShape = SQUARE;

    public Color getIconColor()
    {
        return iconColor;
    }

    public void setIconColor(Color iconColor)
    {
        this.iconColor = iconColor;
    }

    public int getIconShape()
    {
        return iconShape;
    }

    public void setIconShape(int iconShape)
    {
        this.iconShape = iconShape;
    }

    public ImageIcon generateIcon(AbstractCommand forObject)
    {
        char textChar = forObject.getText().charAt(0);
        return new ImageIcon(createIcon(getIconShape(), getIconColor(), textChar));
    }

    private static BufferedImage createIcon(int style, Color color, char text)
    {
        BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = img.createGraphics();
        Color lighterColor = color.brighter().brighter();
        GradientPaint paint = new GradientPaint(0, 0, lighterColor, 16, 16, color);
        g.setPaint(paint);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        Polygon p = new Polygon();
        switch (style)
        {
            case ROUND:
                g.fillOval(0, 0, 16, 16);
                break;
            case SQUARE:
                g.fillRect(0, 0, 16, 16);
                break;
            case DIAMOND:
                p.addPoint(8, 0);
                p.addPoint(16, 8);
                p.addPoint(8, 16);
                p.addPoint(0, 8);
                g.fillPolygon(p);
                break;
            case OCTAGON:
                p.addPoint(0, 5);
                p.addPoint(5, 0);
                p.addPoint(11, 0);
                p.addPoint(16, 5);
                p.addPoint(16, 11);
                p.addPoint(11, 16);
                p.addPoint(5, 16);
                p.addPoint(0, 11);
                g.fillPolygon(p);
                break;
            default:
        }

        g.setFont(new Font("SansSerif", Font.PLAIN, 10));
        g.setColor(Color.white);
        char uppercaseText = Character.toUpperCase(text);
        if (uppercaseText == 'W' || uppercaseText == 'M')
        {
            g.drawString(Character.toString(text), 3f, 11f);
        }
        else
        {
            g.drawString(Character.toString(text), 4.5f, 11f);
        }
        img.flush();
        return img;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy