处理问题:使用 Vectors

问题描述

这里有一个具体的问题。我最近开始阅读 Daniel Shiffman 的书“代码的本质”,并开始练习 Java 和 Processing。从 Vector 章节(第 1 章)的最后一个练习开始,我不知道如何使我正在绘制的圆圈移动到光标处...... 这是 Mover 类;

public class Mover {
PVector location;
PVector acceleration;
PVector veLocity;
double radius;
float topSpeed;

public Mover() {
    location = new PVector(200,200);
    veLocity = new PVector(0,0);
    topSpeed = 4;
}

public void update(){
    float mx = (float) MouseInfo.getPointerInfo().getLocation().getX();
    float my = (float) MouseInfo.getPointerInfo().getLocation().getY();
    PVector mouse = new PVector(mx,my);
    PVector dir = PVector.sub(mouse,location);
    dir.normalize();

     acceleration = dir;
     veLocity.add(acceleration);
     veLocity.limit(topSpeed);
     location.add(veLocity);
}

public void display(GraphicsContext g ){
    g.clearRect(0,1000,800);
    g.setFill(Color.RED);
    g.filloval(location.x,location.y,radius,radius);
}

带有 FXMLLoader 的主类:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{

    Parent root = FXMLLoader.load(getClass().getResource("Noc.fxml"));
    primaryStage.setTitle("Hello World");
    Scene tryout = new Scene(root,800,true);
    primaryStage.setScene(tryout);

    primaryStage.setResizable(false);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

这是主要的 FXML 类:

public class Noc {
        @FXML
    private Canvas can ;
    GraphicsContext g;
    Mover m = new Mover();
@FXML
private void initialize(){
    g = can.getGraphicsContext2D();
    can = new Canvas(1000,800);
    move();

}
private void move(){
    AnimationTimer at = new AnimationTimer() {
        @Override
        public void handle(long l) {
            m.radius = 30;
            m.update();
            m.checkBounds(); //function to check the edges of the canvas
            m.display(g);
        }
    };
    at.start();

}

}

我尝试过的所有其他示例都运行良好,但在这个示例中我似乎遇到了问题。圆圈总是在离鼠标 150-200 像素的地方停止。我将不胜感激任何帮助/建议

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)