ArrowIcon.java :  » XML-UI » gui4j » org » gui4j » core » swing » calendar » Java Open Source

Java Open Source » XML UI » gui4j 
gui4j » org » gui4j » core » swing » calendar » ArrowIcon.java
package org.gui4j.core.swing.calendar;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.Icon;
/**
  * Instances of this class represent an icon displaying an arrow pointing
  * up, down, left or right.
  */
class ArrowIcon implements Icon
{
    public static final int UP = 1;
    public static final int LEFT = 2;
    public static final int DOWN = 3;
    public static final int RIGHT = 4;
    private static final int nPoints = 3;
    private int orientation;
    private Dimension preferredSize = new Dimension(10, 10);
    private int[] xPoints = new int[nPoints];
    private int[] yPoints = new int[nPoints];

    /**
     * Creates an instance of ArrowIcon with the specified arrow orientation.
     * @param orientation defines the direction the arrow points to. Can be:
     * ArrowIcon.UP, ArrowIcon.DOWN, ArrowIcon.LEFT, ArrowIcon.RIGHT
     */
    public ArrowIcon(int orientation)
    {
        this.orientation = orientation;
    }

    /**
    * Sets the size of the icon to the given dimension
    * @param size the new size of the icon
    */
    public void setPreferredSize(Dimension size)
    {
        this.preferredSize = size;
    }

    /**
    * Returns the icon's height.
    * @return the icon's height
    */
    public int getIconHeight()
    {
        return this.preferredSize.height;
    }

    /**
    * Returns the icon's width.
    * @return the icon's width
    */
    public int getIconWidth()
    {
        return this.preferredSize.width;
    }

    /**
    * Draws the icon at the specified location.
    * @param c the component on which the icon should be painted
    * @param g the Graphics instance that should be used to paint
    * @param x the x-position where the icon should to be painted
    * @param y the y-position where the icon should to be painted
    */
    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        int w = this.getIconWidth();
        int h = this.getIconHeight();
        this.recalculateShape(x, y, w, h);
        g.setColor(c.getForeground());
        g.fillPolygon(xPoints, yPoints, nPoints);
    }

    /**
    * Recalculates the shape of the arrow icon accoring to the specified location
    * and dimension.
    * @param x the x-position where the icon should to be painted
    * @param y the y-position where the icon should to be painted
    * @param w the width the icon should have
    * @param h the height the icon should have
    */
    protected void recalculateShape(int x, int y, int w, int h)
    {
        switch (this.orientation)
        {
            case UP :
                xPoints[0] = x;
                xPoints[1] = x + (w - 1) / 2;
                xPoints[2] = x + w - 1;
                yPoints[0] = y + h - 1;
                yPoints[1] = y;
                yPoints[2] = y + h - 1;
                break;
            case LEFT :
                xPoints[0] = x;
                xPoints[1] = x + w - 1;
                xPoints[2] = x + w - 1;
                yPoints[0] = y + (h - 1) / 2;
                yPoints[1] = y;
                yPoints[2] = y + h - 1;
                break;
            case DOWN :
                xPoints[0] = x;
                xPoints[1] = x + (w - 1) / 2;
                xPoints[2] = x + w - 1;
                yPoints[0] = y;
                yPoints[1] = y + h - 1;
                yPoints[2] = y;
                break;
            case RIGHT :
                xPoints[0] = x;
                xPoints[1] = x + w - 1;
                xPoints[2] = x;
                yPoints[0] = y;
                yPoints[1] = y + (h - 1) / 2;
                yPoints[2] = y + h - 1;
                break;
            default :
                break;
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.