简体   繁体   English

Java FX:将窗格中不同对象的坐标与右键单击时的鼠标坐标进行比较

[英]Java FX: comparing the coordinates of different objects in a pane to the coordinates of the mouse when right clicked

I don't know how to compare all of the x and y of the circle objects on pane to those of the mouse. 我不知道如何将窗格上的圆形对象的所有x和y与鼠标的进行比较。 The problem Im working on asks me to to set it so the secondary click of the mouse removes a point when it is placed on it, I figure I can do this by comparing all of the distances of the circles coordinates and the mouse coordinates (using distance formula) to the radi of the circles. 我正在处理的问题要求我进行设置,以便在单击鼠标时将鼠标第二次单击将其移到该点上,我认为可以通过比较圆坐标和鼠标坐标的所有距离(使用距离公式)到圆的半径。 If one of the distances is less than the radi I would remove that circle. 如果距离之一小于半径,我将删除该圆。 The problem is that I dont know how to call all of the points on the pane so I can compare them. 问题是我不知道如何调用窗格上的所有点,因此我无法进行比较。 This is the code I have so far to give you a better understanding of how the points are set up. 到目前为止,这是我可以使您更好地了解如何设置积分的代码。

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

public class Homework6 extends Application {

@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
    // Create a pane and set its properties
    Pane pane = new Pane();
    //Circle circle1 = new Circle(7);
    Circle[] circles = new Circle[0];


    pane.setOnMouseClicked(e -> {
        switch (e.getButton()) {

            case PRIMARY:
                Circle circle1 = new Circle(7);
                circle1.setCenterX(e.getX());
                circle1.setCenterY(e.getY());
                pane.getChildren().add(circle1);
                circle1.setFill(Color.WHITE);
                circle1.setStroke(Color.BLACK);


            case SECONDARY:



        }
    });

    // Create a scene and place the pane in the stage
    Scene scene = new Scene(pane);
    primaryStage.setTitle("KeyEventDemo"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage

    pane.requestFocus(); // text is focused to receive key input
}

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

} }

No need to do any computations! 无需进行任何计算! Method evt.getTarget() should already return the circle that has been clicked. 方法evt.getTarget()应该已经返回了已单击的圆。

Have a look at Oracle's JavaFX tutorials to learn how to handle event. 查看Oracle的JavaFX教程 ,了解如何处理事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM