0

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;

}
gunr2171
  • 16,104
  • 25
  • 61
  • 88

3 Answers3

1

I was trying to add a addMouseListeners() to the run() method and that was the issue. The above solution by me to add a GCanvas and listen to it works too but is not needed. The addMouseListeners() should have rather been initialised using init() and not within run(). I was not able to use init() before as I'm learning Java from Stanfrod CS106A which used a customised Karel.jar library reference. This library had made the init() method final so i was not able to refer to it in any program. After i removed the Karel.jar library and used the acm.jar from jtf i was able to use init() and also able to use the addMouseListeners() directly to the canvas without adding any other canvas on the already drawn canvas. So the whole problem was Karel.jar !

0

The solution was to add a GCanvas object and use addMouseListener() on that object.

        GCanvas c = getGCanvas();   //Creates a GCanvas component so that addMouseListener could be applied to it.
        c.addMouseListener(this);   //Tracks mouse event for the canvas

Then I was able to track mouse events for the canvas by implementing MouseInputListener as told by @madprogrammer .

0
public class RubberBanding extends GraphicsProgram implements MouseListener {
    public void run() {
        GCanvas c = getGCanvas();
        c.addMouseListener(this); 
    }

    @Override 
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseDragged(MouseEvent e) {}

}

You could either implement MouseListener at a class level like I did above. You could also create a separate concrete class that implements it and pass and instance of that to the canvas instead of this. You could even make a field variable equal to an anonymous class that implements the listener and pass it it Canvas object.

MouseListener listener = new MouseListener() {
    @Override 
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseDragged(MouseEvent e) {}
}

class MyMouseListener implements MouseListener {
    @Override 
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseDragged(MouseEvent e) {}
}

public void run() {
    GCanvas c = getGCanvas();
    c.addMouseListener(listener); 

    // OR

    GCanvas c = getGCanvas();
    c.addMouseListener(new MyMouseListener()); 

}
jophde
  • 444
  • 1
  • 5
  • 13