File: ArrowIcon.java

package info (click to toggle)
libswidgets-java 0.1.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 356 kB
  • ctags: 667
  • sloc: java: 3,436; xml: 64; makefile: 11
file content (155 lines) | stat: -rw-r--r-- 4,593 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * ArrowIcon.java
 *
 * Created on 22 July 2002, 21:15
 */

package org.tigris.swidgets;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.io.Serializable;

import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

/**
 * A metal look and feel arrow icon that can be created to point to a
 * compass point.
 *
 * @author  administrator
 */
public class ArrowIcon implements Icon, Serializable, SwingConstants {

    private static final long serialVersionUID = -5251649967037193581L;
    private static final int SIZE = 10;
    private static final int ONE_TOUCH_SIZE = 6;

    // Sprite buffer for the arrow image of the left button
    private int[][] buffer;

    private int[][] northWestBuffer = {
    {0, 0, 0, 2, 2, 0, 0, 0, 0},
    {0, 0, 2, 1, 1, 1, 0, 0, 0},
    {0, 2, 1, 1, 1, 1, 1, 0, 0},
    {2, 1, 1, 1, 1, 1, 1, 1, 0},
    {0, 3, 3, 3, 3, 3, 3, 3, 3}
    };

    private int[][] southEastBuffer = {
    {2, 2, 2, 2, 2, 2, 2, 2, 0},
    {0, 1, 1, 1, 1, 1, 1, 3, 3},
    {0, 0, 1, 1, 1, 1, 3, 3, 0},
    {0, 0, 0, 1, 1, 3, 3, 0, 0},
    {0, 0, 0, 0, 3, 3, 0, 0, 0}
    };

    private int direction;
    private int width = SIZE;
    private int height = SIZE;

    /** Construct an ArrowIcon pointing in the given direction
     *
     * @param direction the direction the arrow will point, this being
     * one of the constants NORTH, SOUTH, EAST, WEST
     */        
    public ArrowIcon(int dir) {
        this.direction = dir;
    }

    /** Paints the icon. The top-left corner of the icon is drawn at
     * the point (x, y) in the coordinate space of the graphics
     * context g. If this icon has no image observer, this method uses
     * the c component as the observer.
     *
     * @param c the component to be used as the observer if this icon
     * has no image observer
     * @param g the graphics context
     * @param x the X coordinate of the icon's top-left corner
     * @param y the Y coordinate of the icon's top-left corner
     */
    public void paintIcon(Component c, Graphics g, int x, int y) {
        int blockSize = Math.min(SIZE, ONE_TOUCH_SIZE);

        // Initialize the color array
        Color[] colors = {
	    c.getBackground(),
	    UIManager.getColor("controlDkShadow"),
	    UIManager.getColor("infoText"),
	    UIManager.getColor("controlHighlight")};

        // Fill the background first ...
        g.setColor(c.getBackground());
        g.fillRect(0, 0, c.getWidth(), c.getHeight());

        // ... then draw the arrow.
        if (c instanceof ArrowButton) {
            ArrowButton button = (ArrowButton) c;
            ButtonModel model = button.getModel();

            if (model.isPressed()) {
                // Adjust color mapping for pressed button state
                colors[1] = colors[2];
            }
        }
        
        if (direction == NORTH || direction == SOUTH) {
            if (direction == NORTH) buffer = northWestBuffer;
            else buffer = southEastBuffer;
            for (int i = 1; i <= buffer[0].length; i++) {
                for (int j = 1; j < blockSize; j++) {
                    if (buffer[j - 1][i - 1] != 0) {
                        g.setColor(colors[buffer[j - 1][i - 1]]);
                        g.drawLine(i, j, i, j);
                    }
                }
            }
        }
        else {
            if (direction == WEST) buffer = northWestBuffer;
            else buffer = southEastBuffer;
            for (int i = 1; i <= buffer[0].length; i++) {
                for (int j = 1; j < blockSize; j++) {
                    if (buffer[j - 1][i - 1] != 0) {
                        g.setColor(colors[buffer[j - 1][i - 1]]);
                        g.drawLine(j, i, j, i);
                    }
                }
            }
        }
    }

    /**
     * Gets the height of the icon.
     * @return the height of the icon
     */ 
    public int getIconWidth() {
        return SIZE;
    }


    /**
     * Gets the height of the icon.
     * @return the height of the icon
     */ 
    public int getIconHeight() {
        return SIZE;
    }
    
    /**
     * @param h the height for the icon
     */
    public void setIconHeight(int h) {
        height = h;
    }
    
    /**
     * @param w the width of the icon
     */
    public void setIconWidth(int w) {
        width = w;
    }
}