001: /*
002: * @(#)EventDemo.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis.demos;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import basis.Builder;
031:
032: public class EventDemo extends Demo implements MouseListener,
033: MouseMotionListener, KeyListener {
034: private int w;
035: private int h;
036: private int border;
037: private int corner;
038: private int cursorOffset;
039: private int cursorHeight;
040: private boolean inside;
041: private int posX;
042: private int posY;
043: private Color[] buttonColors = new Color[3];
044: private int fontSize;
045: private Font font;
046: private int buttonWidth;
047: private int buttonHeight;
048: private String keyCharString = "";
049: private String keyCodeString = "";
050: private String modifierString = "";
051:
052: public EventDemo() {
053: for (int i = 0; i < 3; i++) {
054: buttonColors[i] = Builder.SUN_BLUE;
055: }
056: addMouseListener(this );
057: addMouseMotionListener(this );
058: addKeyListener(this );
059: }
060:
061: public void paint(Graphics g) {
062: Dimension d = getSize();
063: w = d.width - 1;
064: h = d.height - 1;
065: border = (w + h) / 40;
066: corner = (w + h) / 15;
067: cursorOffset = 2 * h / 3;
068: cursorHeight = (h / 3 - border) / 3 - 1;
069: g.setColor(Color.white);
070: g.fillRect(0, 0, w, h);
071: g.setColor(inside ? Builder.SUN_RED : Builder.SUN_BLUE);
072: g.fillRect(0, 0, corner, border);
073: g.fillRect(corner + 1, 0, w - 2 * (corner + 1), border);
074: g.fillRect(w - corner, 0, corner, border);
075: g.fillRect(0, 0, border, corner);
076: g.fillRect(w - border, 0, border, corner);
077: g.fillRect(0, corner + 1, border, h - 2 * (corner + 1));
078: g
079: .fillRect(w - border, corner + 1, border, h - 2
080: * (corner + 1));
081: g.fillRect(0, h - corner, border, corner);
082: g.fillRect(w - border, h - corner, border, corner);
083: g.fillRect(0, h - border, corner, border);
084: g
085: .fillRect(corner + 1, h - border, w - 2 * (corner + 1),
086: border);
087: g.fillRect(w - corner, h - border, corner, border);
088: g.fillRect(border + 1, cursorOffset + 0 * (cursorHeight + 1), w
089: / 2 - border - 2, cursorHeight);
090: g.fillRect(border + 1, cursorOffset + 1 * (cursorHeight + 1), w
091: / 2 - border - 2, cursorHeight);
092: g.fillRect(border + 1, cursorOffset + 2 * (cursorHeight + 1), w
093: / 2 - border - 2, cursorHeight);
094: g.fillRect(w / 2 + 1, cursorOffset + 0 * (cursorHeight + 1), w
095: / 2 - border - 2, cursorHeight);
096: g.fillRect(w / 2 + 1, cursorOffset + 1 * (cursorHeight + 1), w
097: / 2 - border - 2, cursorHeight);
098: g.fillRect(w / 2 + 1, cursorOffset + 2 * (cursorHeight + 1), w
099: / 2 - border - 2, cursorHeight);
100: g.setColor(Color.white);
101: int fontWidth = (w - 2 * border) / 2 / 5;
102: int fontHeight = (h - 2 * border) / 3 / 4;
103: fontSize = fontWidth < fontHeight ? fontWidth : fontHeight;
104: font = new Font("Monospaced", Font.BOLD, fontSize);
105: g.setFont(font);
106: int x = border + 4;
107: int y = cursorOffset + cursorHeight - 4;
108: g.drawString("Default", x, y);
109: y += cursorHeight;
110: g.drawString("Hand", x, y);
111: y += cursorHeight;
112: g.drawString("Text", x, y);
113: x = w / 2 + 4;
114: y = cursorOffset + cursorHeight - 4;
115: g.drawString("Cross", x, y);
116: y += cursorHeight;
117: g.drawString("Move", x, y);
118: y += cursorHeight;
119: g.drawString("Wait", x, y);
120: paintMouseButtons(g);
121: paintMousePosition(g);
122: paintKeyStrings(g);
123: }
124:
125: public void mouseClicked(MouseEvent e) {
126: requestFocus();
127: }
128:
129: public void mouseEntered(MouseEvent e) {
130: Toolkit.getDefaultToolkit().beep();
131: requestFocus();
132: inside = true;
133: repaint();
134: }
135:
136: public void mouseExited(MouseEvent e) {
137: inside = false;
138: repaint();
139: }
140:
141: public void mousePressed(MouseEvent e) {
142: Toolkit.getDefaultToolkit().beep();
143: Color color = Builder.SUN_RED;
144: int modifier = e.getModifiers();
145: if ((modifier & InputEvent.BUTTON1_MASK) != 0) {
146: buttonColors[0] = color;
147: }
148: if ((modifier & InputEvent.BUTTON2_MASK) != 0) {
149: buttonColors[1] = color;
150: }
151: if ((modifier & InputEvent.BUTTON3_MASK) != 0) {
152: buttonColors[2] = color;
153: }
154: Graphics g = getGraphics();
155: paintMouseButtons(g);
156: }
157:
158: public void mouseReleased(MouseEvent e) {
159: Color color = Builder.SUN_BLUE;
160: int modifier = e.getModifiers();
161: if ((modifier & InputEvent.BUTTON1_MASK) != 0) {
162: buttonColors[0] = color;
163: }
164: if ((modifier & InputEvent.BUTTON2_MASK) != 0) {
165: buttonColors[1] = color;
166: }
167: if ((modifier & InputEvent.BUTTON3_MASK) != 0) {
168: buttonColors[2] = color;
169: }
170: Graphics g = getGraphics();
171: paintMouseButtons(g);
172: }
173:
174: public void mouseDragged(MouseEvent e) {
175: mouseMoved(e);
176: }
177:
178: public void mouseMoved(MouseEvent e) {
179: posX = e.getX();
180: posY = e.getY();
181: Graphics g = getGraphics();
182: paintMousePosition(g);
183: Cursor cursor = Cursor
184: .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
185: if (posX >= 0 && posX <= w && posY >= 0 && posY <= h) {
186: if (posX > corner && posX < w - (corner + 1)
187: && posY < border) {
188: cursor = Cursor
189: .getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
190: }
191: if (posX > corner && posX < w - (corner + 1)
192: && posY > h - (border + 1)) {
193: cursor = Cursor
194: .getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
195: }
196: if (posX > w - (border + 1) && posY > corner
197: && posY < h - (corner + 1)) {
198: cursor = Cursor
199: .getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
200: }
201: if (posX < border && posY > corner
202: && posY < h - (corner + 1)) {
203: cursor = Cursor
204: .getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
205: }
206: if (posX < corner && posY < border || posX < border
207: && posY < corner) {
208: cursor = Cursor
209: .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
210: }
211: if (posX > w - (corner + 1) && posY < border
212: || posX > w - (border + 1) && posY < corner) {
213: cursor = Cursor
214: .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
215: }
216: if (posX > w - (corner + 1) && posY > h - (border + 1)
217: || posX > w - (border + 1)
218: && posY > h - (corner + 1)) {
219: cursor = Cursor
220: .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
221: }
222: if (posX < corner && posY > h - (border + 1)
223: || posX < border && posY > h - (corner + 1)) {
224: cursor = Cursor
225: .getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
226: }
227: if (posX > border && posX < w / 2 - 1
228: && posY > cursorOffset + 0 * cursorHeight + 1
229: && posY < cursorOffset + 1 * cursorHeight + 1) {
230: cursor = Cursor
231: .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
232: }
233: if (posX > border && posX < w / 2 - 1
234: && posY > cursorOffset + 1 * cursorHeight + 1
235: && posY < cursorOffset + 2 * cursorHeight + 1) {
236: cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
237: }
238: if (posX > border && posX < w / 2 - 1
239: && posY > cursorOffset + 2 * cursorHeight + 1
240: && posY < cursorOffset + 3 * cursorHeight + 1) {
241: cursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
242: }
243: if (posX > w / 2 && posX < w - (border + 1)
244: && posY > cursorOffset + 0 * cursorHeight + 1
245: && posY < cursorOffset + 1 * cursorHeight + 1) {
246: cursor = Cursor
247: .getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
248: }
249: if (posX > w / 2 && posX < w - (border + 1)
250: && posY > cursorOffset + 1 * cursorHeight + 1
251: && posY < cursorOffset + 2 * cursorHeight + 1) {
252: cursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
253: }
254: if (posX > w / 2 && posX < w - (border + 1)
255: && posY > cursorOffset + 2 * cursorHeight + 1
256: && posY < cursorOffset + 3 * cursorHeight + 1) {
257: cursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
258: }
259: }
260: setCursor(cursor);
261: }
262:
263: public void keyPressed(KeyEvent e) {
264: Toolkit.getDefaultToolkit().beep();
265: keyCharString = "";
266: keyCodeString = "";
267: modifierString = "";
268: char keyChar = e.getKeyChar();
269: if (keyChar == KeyEvent.CHAR_UNDEFINED) {
270: keyCharString += "undefined";
271: } else {
272: keyCharString += "'" + keyChar + "'";
273: keyCharString += " (" + (int) keyChar + ")";
274: }
275: int keyCode = e.getKeyCode();
276: Integer key = new Integer(keyCode);
277: String value = e.getKeyText(keyCode);
278: if (value == null) {
279: value = "unknown";
280: }
281: keyCodeString += value + " (" + keyCode + ")";
282: int modifier = e.getModifiers();
283: if ((modifier & InputEvent.SHIFT_MASK) != 0) {
284: if (keyCode != KeyEvent.VK_SHIFT) {
285: modifierString += "SHIFT ";
286: }
287: }
288: if ((modifier & InputEvent.CTRL_MASK) != 0) {
289: if (keyCode != KeyEvent.VK_CONTROL) {
290: modifierString += "CONTROL ";
291: }
292: }
293: if ((modifier & InputEvent.ALT_MASK) != 0) {
294: if (keyCode != KeyEvent.VK_ALT) {
295: modifierString += "ALT ";
296: }
297: }
298: if ((modifier & InputEvent.ALT_GRAPH_MASK) != 0) {
299: if (keyCode != KeyEvent.VK_ALT_GRAPH) {
300: modifierString += "ALT_GRAPH ";
301: }
302: }
303: if ((modifier & InputEvent.META_MASK) != 0) {
304: if (keyCode != KeyEvent.VK_META) {
305: modifierString += "META ";
306: }
307: }
308: Graphics g = getGraphics();
309: paintKeyStrings(g);
310: }
311:
312: public void keyReleased(KeyEvent e) {
313: }
314:
315: public void keyTyped(KeyEvent e) {
316: }
317:
318: private void paintMouseButtons(Graphics g) {
319: buttonWidth = (w - 2 * (border + 1)) / 3 - 1;
320: buttonHeight = (h - 2 * border) / 3;
321: for (int i = 0; i < 3; i++) {
322: g.setColor(buttonColors[i]);
323: g.fillRect(border + 1 + i * (buttonWidth + 1), border + 1,
324: buttonWidth, buttonHeight);
325: }
326: }
327:
328: private void paintMousePosition(Graphics g) {
329: int x = border;
330: int y = border + 1 + buttonHeight;
331: g.setColor(Color.white);
332: g.fillRect(x, y, w - 2 * border, fontSize);
333: g.setFont(font);
334: g.setColor(Color.black);
335: x += 1;
336: y += 4 * fontSize / 5;
337: g.drawString("x:", x, y);
338: g.drawString("y:", w / 2, y);
339: g.setColor(inside ? Builder.SUN_RED : Builder.SUN_BLUE);
340: g.drawString(" " + posX, x, y);
341: g.drawString(" " + posY, w / 2, y);
342: }
343:
344: private void paintKeyStrings(Graphics g) {
345: g.setColor(Color.white);
346: int x = border;
347: int y = border + 1 + buttonHeight + fontSize;
348: g.fillRect(x, y, w - 2 * border, 3 * fontSize);
349: g.setFont(font);
350: x += 1;
351: y += fontSize;
352: g.setColor(Color.black);
353: g.drawString("char: ", x, y - fontSize / 5);
354: g.setColor(inside ? Builder.SUN_RED : Builder.SUN_BLUE);
355: g.drawString(" " + keyCharString, x, y - fontSize / 5);
356: y += fontSize;
357: g.setColor(Color.black);
358: g.drawString("code: ", x, y - fontSize / 5);
359: g.setColor(inside ? Builder.SUN_RED : Builder.SUN_BLUE);
360: g.drawString(" " + keyCodeString, x, y - fontSize / 5);
361: y += fontSize;
362: g.setColor(Color.black);
363: g.drawString("mods: ", x, y - fontSize / 5);
364: g.setColor(inside ? Builder.SUN_RED : Builder.SUN_BLUE);
365: g.drawString(" " + modifierString, x, y - fontSize / 5);
366: g.setColor(Builder.SUN_RED);
367: }
368: }
|