Posted on Leave a comment

Drawline.java solution

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]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.