Mouse events with java.
[code lang=”java”]/*
* DrawLines.java
* draw lines in canvas
* ralph @today
*
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;
public class Drawlines extends GraphicsProgram {
public void run(){
/* Tell Java that we’re ready for the mouse */
addMouseListeners();
}
public void mousePressed(MouseEvent e){
// start the line with a mousepressed event
line = new GLine(e.getX(),e.getY(),e.getX(),e.getY());
add(line);
}
public void mouseDragged(MouseEvent e){
/* end the line where we click */
line.setEndPoint(e.getX(), e.getY());
}
/* private instance variables */
private GLine line;
}[/code]