/* * Demonstrate how to handle user input * In particular, react to mouse clicks * * written by mike slattery - jan 2001 */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class EventDemo extends Applet { boolean mdown; // true when mouse button is pressed int mx,my; // coordinates of most recent mouse click /* * Define a mouse listener to say how to * react to mouse events */ private class mseL implements MouseListener { public void mousePressed(MouseEvent e) { mdown = true; mx = e.getX(); my = e.getY(); repaint(); } public void mouseReleased(MouseEvent e) { mdown = false; repaint(); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} } public void init() { setBackground(Color.blue); mdown = false; /* * Tell the applet that the mouse * listener object should be notified * about mouse events */ addMouseListener(new mseL()); } public void paint(Graphics g) { /* * If the mouse isn't pressed, just * leave the plain background. * If it is pressed, draw a dot * there. */ if (mdown) { g.setColor(Color.green); g.fillOval(mx-10,my-10,20,20); } } }