1 /* 2 * EventTest Java Program 3 */ 4 5 import java.applet.*; 6 import java.awt.*; 7 8 public class EventTest extends Applet { 9 int maxObj = 3; /* max object number */ 10 mPoint object[]; /* objecct array */ 11 Image img; 12 int current = -1, hit = -1; 13 int xofs = 0, yofs = 0; 14 public void init() { 15 /* initial object array */ 16 setBackground(Color.black); 17 setForeground(Color.white); 18 object = new mPoint[maxObj]; 19 /* initial a rectangle */ 20 object[0] = new mRectangle(10, 10, 100, 100); 21 object[0].setColor(Color.red); 22 /* initial a circle */ 23 object[1] = new mCircle(200, 10, 100, 100); 24 object[1].setColor(Color.red); 25 /* initial a triangle */ 26 object[2] = new mTriangle(10, 200, 100, 100); 27 object[2].setColor(Color.red); 28 img = createImage(size().width, size().height); 29 } 30 /* this method will be called to repond a repaint */ 31 public void paint(Graphics g) { update(g);} 32 /* this method will be called to repond a disclosure */ 33 public void update(Graphics g) { 34 /* Get Off Screen Graphics Context */ 35 Graphics og = img.getGraphics(); 36 /* fill background */ 37 og.setColor(getBackground()); 38 og.fillRect(1, 1, size().width-2, size().height-2); 39 /* draw boundry */ 40 og.setColor(getForeground()); 41 og.drawRect(0, 0, size().width-1, size().height-1); 42 /* draw objects */ 43 for (int i=0; i<maxObj; i++) 44 object[i].paint(og); 45 /* user who call getGraphics should call dispose */ 46 og.dispose(); 47 g.drawImage(img, 0, 0, this); 48 } 49 /* Mouse Move Event */ 50 public boolean mouseMove(Event ev, int x, int y) { 51 int i; 52 hit = -1; 53 for (i=0; i<maxObj; i++) { 54 if ( object[i].isInside(x, y) ) { 55 hit = i; 56 break; 57 } 58 } 59 for (i=0; i<maxObj; i++) 60 object[i].setColor((i==hit) ? Color.cyan : Color.red); 61 repaint(); 62 return true; 63 } 64 /* Mouse Down Event */ 65 public boolean mouseDown(Event ev, int x, int y) { 66 current = hit; 67 if ( current != -1 ) { 68 xofs = object[current].x-x; 69 yofs = object[current].y-y; 70 } 71 return true; 72 } 73 /* Mouse Drag Event */ 74 public boolean mouseDrag(Event ev, int x, int y) { 75 if ( current != -1 ) { 76 object[current].moveTo(x+xofs, y+yofs); 77 repaint(); 78 } 79 return true; 80 } 81 /* Mouse Up Event */ 82 public boolean mouseUp(Event ev, int x, int y) { 83 current = -1; 84 return true; 85 } 86 /* Mouse Enter Event */ 87 public boolean mouseEnter(Event ev, int x, int y) { 88 setForeground(Color.cyan); 89 repaint(); 90 return true; 91 } 92 /* Mouse Exit Event */ 93 public boolean mouseExit(Event ev, int x, int y) { 94 setForeground(Color.red); 95 repaint(); 96 return true; 97 } 98 /* KeyDown Event */ 99 public boolean keyDown(Event ev, int key) { 100 if ( hit == -1 ) return false; 101 int dx = 0, dy = 0; 102 switch ( key ) { 103 case Event.DOWN: 104 dy = 1; 105 break; 106 case Event.UP: 107 dy = -1; 108 break; 109 case Event.LEFT: 110 dx = -1; 111 break; 112 case Event.RIGHT: 113 dx = +1; 114 break; 115 } 116 if ((dx != 0) || (dy != 0)) { 117 object[hit].move(dx, dy); 118 repaint(); 119 } 120 return true; 121 } 122 }