Interaction: Mouse and Keyboard Input

You now have the tools to draw a lot of really cool animations and images on your web pages. This alone puts you leaps and bounds beyond the average web page designer. Still that's only half the point of applets. The other half is interaction with the user. Your applets can accept input from the user and respond to them. For the first time a web surfer can move beyond mere browsing to genuine participation.

Mouse Input: Java Doodle

Here's a simple applet that lets you doodle with the mouse on an applet.

import java.applet.Applet; import java.awt.*; import java.util.Vector; public class JavaDoodle extends Applet { Vector points = new Vector(); public void paint(Graphics g) { int x1, y1, x2, y2; Point tempPoint; if (points.size() 1) { tempPoint = (Point) points.elementAt(0); x1 = tempPoint.x; y1 = tempPoint.y; for (int i = 1; i < points.size(); i++) { tempPoint = (Point) points.elementAt(i); x2 = tempPoint.x; y2 = tempPoint.y; g.drawLine(x1, y1, x2, y2); x1 = x2; y1 = y2; } // end for } // end if } public boolean mouseDown(Event e, int x, int y) { points.addElement(new Point(x, y)); return true; } public boolean mouseDrag(Event e, int x, int y) { points.addElement(new Point(x, y)); repaint(); return true; } public boolean mouseUp(Event e, int x, int y) { points.addElement(new Point(x, y)); repaint(); return true; } }

Exercises
  1. Revise the applet so that it doesn't draw a line between the point where the mouse button was released and the point where it was pressed again.

 

Bhopal news
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

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india