import javax.swing.Icon;
import java.awt.*;

        // An small arrow icon. Four possible directions.
class ArrowIcon implements Icon {
    public static final Color defaultColor = Color.blue;
    public static final int LEFT = 0, RIGHT = 1, UP = 2, DOWN = 3;
    public static final int defaultDirection = LEFT;

    public class IllegalDirectionException extends RuntimeException {
        public IllegalDirectionException(int d){
            super("Unknown direction: "+d);
        }
    }

    public ArrowIcon() throws IllegalDirectionException {
        this(defaultDirection,defaultColor);
    }

    public ArrowIcon(int direction) throws IllegalDirectionException {
        this(direction,defaultColor);
    }

    public ArrowIcon(Color c) throws IllegalDirectionException {
        this(defaultDirection,c);
    }

    public ArrowIcon(int direction,Color c) throws IllegalDirectionException {
        switch(direction){
            case LEFT: case RIGHT: case UP: case DOWN:
                break;
            default:
                throw new IllegalDirectionException(direction);
        }
        this.direction = direction;
        this.color = c;
    }

        // The Icon interface.
    public int getIconHeight(){
        return 3*length;
    }

    public int getIconWidth(){
        return 3*length;
    }

        // Attempt to center the Icon within the component.
    public void paintIcon(Component c, Graphics g, int x, int y){
        final Dimension size = c.getSize();
        int availableWidth = size.width-2*x,
            availableHeight = size.height-2*y;
        g.setColor(color);
        switch(getDirection()){
            case LEFT:
                x += (availableWidth-length)/2;
                y += availableHeight/2;
                g.drawLine(x,y,x+length,y);
                g.drawLine(x,y,x+lengthOffset,y-widthOffset);
                g.drawLine(x,y,x+lengthOffset,y+widthOffset);
                break;
            case RIGHT:
                x += (availableWidth-length)/2+length;
                y += availableHeight/2;
                g.drawLine(x,y,x-length,y);
                g.drawLine(x,y,x-lengthOffset,y-widthOffset);
                g.drawLine(x,y,x-lengthOffset,y+widthOffset);
                break;
            case UP:
                x += availableWidth/2;
                y += (availableHeight-length)/2;
                g.drawLine(x,y,x,y+length);
                g.drawLine(x,y,x-widthOffset,y+lengthOffset);
                g.drawLine(x,y,x+widthOffset,y+lengthOffset);
                break;
            case DOWN:
                x += availableWidth/2;
                y += (availableHeight-length)/2+length;
                g.drawLine(x,y,x,y-length);
                g.drawLine(x,y,x-widthOffset,y-lengthOffset);
                g.drawLine(x,y,x+widthOffset,y-lengthOffset);
                break;
        }
    }

    public int getDirection(){
        return direction;
    }

    private final int direction;
    private final Color color;
    private static final int length = 10, offset = 4;
    private static final double angle = Math.toRadians(45);
    private static final int lengthOffset = (int) (offset*Math.cos(angle)),
                             widthOffset = (int) (offset*Math.sin(angle));
}
