0
사용자가 캔버스에 선을 그릴 수있는 GraphicsProgram을 작성하려고합니다. 마우스 버튼을 누르면 선의 시작점이 설정됩니다. 끌기가 진행됨에 따라 마우스를 드래그하면 다른 끝점이 움직입니다. 마우스를 놓으면 현재 위치에 선이 고정되고 새 선을 시작할 준비가됩니다.간단한 그래픽 상호 작용
내가 코드를 실행할 때 줄을 표시 할 수 없으며 또한 내가 첨부 한 올바른 코드가 어떤 것이 좋을지 설명 할 수 있습니까?
내 코드 :`
import acm.program.*;
import java.awt.event.MouseEvent;
import acm.graphics.*;
public class DrawLines extends GraphicsProgram{
public void init(){
addMouseListeners();
line=new GLine(x1,y1,x2,y2);
}
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
add(line);
}
private GLine line;
private int x1;
private int y1;
private int x2;
private int y2;
}
올바른 코드 : 첫 번째 프로그램은 오직 초기화되지 않은 int
필드는 0으로 초기화되어 있기 때문에 어느 하나 GLine
를 생성
import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;
/** This class allows users to drag lines on the canvas */
public class RubberBanding extends GraphicsProgram {
public void run() {
addMouseListeners();
}
/** 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;
}
해당 GraphicsProgram 클래스는 실행 가능을 구현합니까? –