Stanford CS106Aの Rubber Banding 問題を解決しようとしています。addMouseListener() の Java Doc に従って、それを使用するにはリスナーが必要です。この問題の解決策によると、リスナーは使用されませんが、リスナーなしで使用すると、次のエラーが発生します。
タイプ Component のメソッド addMouseListener(MouseListener) は、arguments() には適用されません。
キャンバス全体をリッスンするようにリスナーを作成するにはどうすればよいでしょうか?
/**
* 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;
}