用鼠标点击点画圆(JavaFX)

Draw circle with mouse click points (JavaFX)

在你阅读之前这是我的家庭作业,所以问题的要求会很具体。

我正在编写一个程序,单击它会在 canvas 上画一条线,并显示从您拖动鼠标的位置开始显示的圆圈。我的代码所做的是,它单击以开始一个点,无论您拖动鼠标并放开的地方都是圆圈。但是,我希望我的圆从选定的点开始(单击并释放鼠标),然后将鼠标和圆拖动到它所在的位置,当再次单击鼠标时,它会将圆固定在 canvas 上。我尝试的是制作两个鼠标事件侦听器,一个用于初始化圆圈,一个用于结束圆圈的半径。但这没有用。我也试过将 getX() 和 getY() 放入主要方法中,但这也没有用。

这是我的代码

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.shape.Line;


public class Main extends Application {
    private Circle circle;

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();
        circle = new Circle(-10,-10,-10);
        circle.setFill(Color.TRANSPARENT);
        circle.setStroke(Color.BLACK);

        root.getChildren().add(circle);

        root.setOnMousePressed(new MousePressEventHandler());
        root.setOnMouseDragged(new MouseDragEventHandler());

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setTitle("blank");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private class MousePressEventHandler implements EventHandler<MouseEvent> {
        @Override
        public void handle(MouseEvent e) {

            circle.setCenterX(e.getX());
            circle.setCenterY(e.getY());


            circle.setRadius(e.getX());
            circle.setRadius(e.getY());

        }
    }

    private class MouseDragEventHandler implements EventHandler<MouseEvent> {
        @Override
        public void handle(MouseEvent e) {
            circle.setRadius(e.getX());
            circle.setRadius(e.getY());

        }
    }


    public static void main(String[] args) {

        Application.launch(args);
    }
}

这是我的做法,扩展了我之前的评论。我测试了这个,它根据你的描述工作。我处理事件的方式略有不同,只是因为我喜欢能够将我的事件添加到场景的特定实例中。你应该能够按照自己的方式实现它,没问题。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Test extends Application {

    private Circle circle;
    private boolean firstClick = true;
    private double centerX, centerY;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();
    
        circle = new Circle();
        circle.setFill(Color.TRANSPARENT);
        circle.setStroke(Color.BLACK);
    
        root.getChildren().add(circle);        

        Scene scene = new Scene(root, 600, 600);
        setHandlers(scene);
    
        primaryStage.setTitle("blank");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void setHandlers(Scene scene) {
        scene.setOnMouseClicked(
            e -> {
                // first instance of clicking the mouse will set the center of the circle
                if (firstClick) { 
                    // you need to keep references of the your circle center when moving the mouse
                    centerX = e.getX();
                    centerY = e.getY();
                    // sets the center
                    circle.setCenterX(centerX);
                    circle.setCenterY(centerY);
                    // sets next "stage" of drawing your circle
                    firstClick = false;
                // on second click will reset the process by setting firstClick to true
                } else {
                    firstClick = true;
                }
            }
        );
    
        scene.setOnMouseMoved(
            e -> {
                // will only evaluate on the first instance of a click
                if (!firstClick) {
                    // Distance formula between center of circle and mouse pointer
                    double c = Math.sqrt(Math.pow(centerX - e.getX(), 2) + 
                                     Math.pow(centerY - e.getY(), 2));
                    circle.setRadius(c);
                }
            }
        );
    }
}