I'm trying to solve Rubber Banding problem of Stanford CS106A. As per Java Doc on addMouseListener() one needs a listener to use it. As per the solution to this problem no listener is used but when I use it without any listener I get the following error :
The method addMouseListener(MouseListener) in the type Component is not applicable for the arguments()
How could a listener be created so that it listens to the canvas as a whole ?
/**
* This program allows users to create lines on the graphics canvas by clicking
* and dragging the mouse. The line is redrawn from the original point to the new
* end point, which makes it look as if it is connected with a rubber band.
*/
package Section_3;
import java.awt.Color;
import java.awt.event.MouseEvent;
import acm.graphics.GLine;
import acm.program.GraphicsProgram;
public class RubberBanding extends GraphicsProgram{
private static final long serialVersionUID = 406328537784842360L;
public static final int x = 20;
public static final int y = 30;
public static final int width = 100;
public static final int height = 100;
public static final Color color = Color.RED;
public void run(){
addMouseListener();
}
/** Called on mouse press to create a new line */
public void mousePressed(MouseEvent e) {
double x = e.getX();
double y = e.getY();
line = new GLine(x, y, x, y);
add(line);
}
/** Called on mouse drag to reset the endpoint */
public void mouseDragged(MouseEvent e) {
double x = e.getX();
double y = e.getY();
line.setEndPoint(x, y);
}
/* Private instance variables */
private GLine line;
}