001: package example;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005:
006: import javax.swing.JPanel;
007: import javax.swing.border.EmptyBorder;
008:
009: /**
010: * Source code for Tutorial 1.
011: * Simple arrow button that fires one event when first clicked, then sends a
012: * stream of events while held down.
013: */
014:
015: public class ArrowButton extends JPanel {
016: /** Directions. */
017: public static final String LEFT = "LEFT", RIGHT = "RIGHT",
018: UP = "UP", DOWN = "DOWN";
019:
020: private String direction;
021: private String lastPaintedDirection;
022: private static ArrowRoller roller;
023: private boolean down;
024: private boolean in;
025: private ActionListener listeners;
026: private static final int SIZE = 12;
027: private static final int BORDER = 2;
028:
029: private static class ArrowRoller extends Thread {
030: /** How long to wait before repeating. */
031: private static final int DELAY_MS = 400;
032: /** How long between repeated firings. */
033: private static final int REPEAT_MS = 100;
034: private boolean die = false;
035: private ArrowButton target;
036: private int state;
037:
038: ArrowRoller() {
039: super ("ArrowRoller");
040: setDaemon(true);
041: }
042:
043: synchronized void abort() {
044: die = true;
045: notify();
046: target = null;
047: ++state;
048: }
049:
050: synchronized void addListener(ArrowButton t) {
051: target = t;
052: ++state;
053: notify();
054: }
055:
056: synchronized void removeListener(ArrowButton t) {
057: if (target == t) {
058: target = null;
059: ++state;
060: }
061: }
062:
063: public void run() {
064: while (!die) {
065: try {
066: ArrowButton target;
067: int state;
068: synchronized (this ) {
069: while ((target = this .target) == null)
070: wait();
071: state = this .state;
072: }
073: sleep(DELAY_MS);
074: while (state == this .state) {
075: target.fireActionEvent();
076: sleep(REPEAT_MS);
077: }
078: } catch (InterruptedException ex) {
079: }
080: }
081: }
082: }
083:
084: public ArrowButton() {
085: this (LEFT);
086: }
087:
088: public ArrowButton(String direction) {
089: enableEvents(AWTEvent.MOUSE_EVENT_MASK);
090: setBorder(new EmptyBorder(BORDER, BORDER, BORDER, BORDER));
091: lastPaintedDirection = direction;
092: setDirection(direction);
093: }
094:
095: public void setDirection(String d) {
096: if (d.equals(LEFT))
097: direction = LEFT;
098: else if (d.equals(RIGHT))
099: direction = RIGHT;
100: else if (d.equals(UP))
101: direction = UP;
102: else
103: direction = DOWN;
104: repaint();
105: }
106:
107: public String getDirection() {
108: return direction;
109: }
110:
111: public Dimension getMinimumSize() {
112: return getPreferredSize();
113: }
114:
115: public Dimension getPreferredSize() {
116: return new Dimension(SIZE, SIZE);
117: }
118:
119: public Dimension getMaximumSize() {
120: return getPreferredSize();
121: }
122:
123: public void update(Graphics g) {
124: if (lastPaintedDirection == direction) {
125: paint(g);
126: } else {
127: super .update(g);
128: lastPaintedDirection = direction;
129: }
130: }
131:
132: public void paint(Graphics g) {
133: super .paint(g);
134: int w = getSize().width, h = getSize().height;
135: g.setColor((in && down) ? getBackground().brighter()
136: : getBackground().darker());
137: if (direction == LEFT) {
138: g.drawLine(w - 2, 2, w - 2, h - 2);
139: g.drawLine(w - 2, h - 2, 2, (h - 2) / 2);
140: g.setColor((in && down) ? getBackground().darker()
141: : getBackground().brighter());
142: g.drawLine(2, (h - 2) / 2, w - 2, 2);
143: } else if (direction == RIGHT) {
144: g.drawLine(2, 2, w - 2, (h - 2) / 2);
145: g.drawLine(2, h - 2, w - 2, (h - 2) / 2);
146: g.setColor((in && down) ? getBackground().darker()
147: : getBackground().brighter());
148: g.drawLine(2, 2, 2, h - 2);
149: } else if (direction == UP) {
150: g.drawLine(2, h - 2, w - 2, h - 2);
151: g.drawLine(w - 2, h - 2, (w - 2) / 2, 2);
152: g.setColor((in && down) ? getBackground().darker()
153: : getBackground().brighter());
154: g.drawLine(2, h - 2, (w - 2) / 2, 2);
155: } else {
156: g.drawLine((w - 2) / 2, h - 2, w - 2, 2);
157: g.setColor((in && down) ? getBackground().darker()
158: : getBackground().brighter());
159: g.drawLine(2, 2, (w - 2) / 2, h - 2);
160: g.drawLine(2, 2, w - 2, 2);
161: }
162: }
163:
164: protected synchronized ArrowRoller getRoller() {
165: if (roller == null) {
166: roller = new ArrowRoller();
167: roller.start();
168: }
169: return roller;
170: }
171:
172: public synchronized void destroyRoller() {
173: roller.abort();
174: roller = null;
175: }
176:
177: protected void processMouseEvent(MouseEvent e) {
178: ArrowRoller roller = getRoller();
179: switch (e.getID()) {
180: case MouseEvent.MOUSE_PRESSED:
181: fireActionEvent();
182: roller.addListener(this );
183: down = in = true;
184: repaint();
185: break;
186: case MouseEvent.MOUSE_RELEASED:
187: roller.removeListener(this );
188: down = in = false;
189: repaint();
190: break;
191: case MouseEvent.MOUSE_ENTERED:
192: in = true;
193: if (down) {
194: roller.addListener(this );
195: repaint();
196: }
197: break;
198: case MouseEvent.MOUSE_EXITED:
199: in = false;
200: if (down) {
201: roller.removeListener(this );
202: repaint();
203: }
204: break;
205: }
206: super .processMouseEvent(e);
207: }
208:
209: public void addActionListener(ActionListener l) {
210: listeners = AWTEventMulticaster.add(l, listeners);
211: }
212:
213: public void removeActionListener(ActionListener l) {
214: listeners = AWTEventMulticaster.remove(l, listeners);
215: }
216:
217: protected void fireActionEvent() {
218: if (listeners != null) {
219: ActionEvent ev = new ActionEvent(this,
220: ActionEvent.ACTION_PERFORMED, direction);
221: listeners.actionPerformed(ev);
222: }
223: }
224: }
|