//Mousing2.java generic mouse actions import java.awt.*; import java.awt.event.*; //MouseAdapter MouseMotionAdapter import javax.swing.*; public class Mousing2 extends JApplet { int x, y; int xPrev, yPrev; int connectDots; public void init() { connectDots = JOptionPane.showConfirmDialog(null,"Connect the dots?", "connect the dots?", JOptionPane.YES_NO_OPTION); //incantation that responds to mouse click events: addMouseListener( //anonymous inner class overrides mouseClicked. //creates a $1 class file (which must be uploaded to web site) new MouseAdapter() { public void mouseClicked( MouseEvent event) { x = event.getX(); //gets X coord of mouse click event y = event.getY(); //to determine which mouse button clicked: if (event.getButton() == MouseEvent.BUTTON1) //left showStatus("Left ("+x+","+y+") BUTTON1"); else if (event.getButton() == MouseEvent.BUTTON2) //middle showStatus("Middle ("+x+","+y+") BUTTON2"); else if (event.getButton() == MouseEvent.BUTTON3) //right showStatus("Right ("+x+","+y+") BUTTON3"); repaint(); //will invoke paint } } ); //incantation that responds to mouse move events: addMouseMotionListener( //anonymous inner class overrides mouseMoved //creates a $2 class file which must be uploaded to web site new MouseMotionAdapter() { public void mouseMoved( MouseEvent event) { showStatus("("+event.getX()+","+event.getY()+")"); } //repaint for fun :) } ); } //without super.paint(g) does not clear the screen each call. public void paint ( Graphics g ) { g.setColor(Color.RED); g.fillOval(x-2,y-2,3,3); //this point if (connectDots == JOptionPane.YES_OPTION) { g.setColor(Color.BLACK); if (xPrev != 0) //only if not first point g.drawLine(x,y,xPrev,yPrev); xPrev = x; yPrev = y; } } } /* moveCount time or distance fires move event? */