如果鼠标单击事件发生在现有圆圈之外,我希望鼠标单击事件拖动现有圆圈或创建新圆圈。现在代码将拖动圆圈,但也会在另一个圆圈的顶部创建一个新圆圈。我真的很感激有关清理代码并使其完成其中一项的任何帮助。这是代码。我一直试图做各种不同的事情让它发挥作用,但我无法弄清楚如何让它只做一个或另一个。
import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.control.ChoiceBox;
import javafx.collections.FXCollections;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
public class Main extends Application {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Drawing Operations Test");
Group root = new Group();
Pane canvas = new Pane();
canvas.setStyle("-fx-background-color: black;");
canvas.setPrefSize(200,200);
Rectangle rectangle = new Rectangle(100,100,Color.RED);
rectangle.relocate(70,70);
canvas.getChildren().add(rectangle);
canvas.addEventHandler(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
Object source = e.getSource();
if (!(source instanceof Circle)) {
Circle circle = new Circle(20,Color.BLUE);
circle.setCursor(Cursor.MOVE);
circle.setOnMousePressed(circleOnMousePressedEventHandler);
circle.setOnMouseDragged(circleOnMouseDraggedEventHandler);
circle.relocate((e.getX()-10),(e.getY()-10));
canvas.getChildren().add(circle);
System.out.println(source);
}
}
});
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
EventHandler<MouseEvent> circleOnMousePressedEventHandler =
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
orgSceneX = t.getSceneX();
orgSceneY = t.getSceneY();
orgTranslateX = ((Circle)(t.getSource())).getTranslateX();
orgTranslateY = ((Circle)(t.getSource())).getTranslateY();
System.out.println(t.getSource());
}
};
EventHandler<MouseEvent> circleOnMouseDraggedEventHandler =
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
((Circle)(t.getSource())).setTranslateX(newTranslateX);
((Circle)(t.getSource())).setTranslateY(newTranslateY);
}
};
}
答案 0 :(得分:0)
我找到了!对我来说,答案是创建一个if else语句并在圈子上使用该事件。我确信有一种更优雅的方式可以做到这一点,但我在Java中并不太优雅.....。
public class Main extends Application {
double orgSceneX, orgSceneY;
double orgTranslateX, orgTranslateY;
Pane canvas;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Drawing Operations Test");
Group root = new Group();
canvas = new Pane();
canvas.setStyle("-fx-background-color: black;");
canvas.setPrefSize(200,200);
Rectangle rectangle = new Rectangle(100,100,Color.RED);
rectangle.relocate(70,70);
canvas.getChildren().add(rectangle);
canvas.setOnMousePressed(MousePressedEventHandler);
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
EventHandler<MouseEvent> MousePressedEventHandler =
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (e.getSource() instanceof Circle) {
orgSceneX = e.getSceneX();
orgSceneY = e.getSceneY();
orgTranslateX = ((Circle)(e.getSource())).getTranslateX();
orgTranslateY = ((Circle)(e.getSource())).getTranslateY();
e.consume();
System.out.println(e.getSource());
}
else {
Circle circle = new Circle(20,Color.BLUE);
circle.setCursor(Cursor.MOVE);
circle.setOnMousePressed(MousePressedEventHandler);
circle.setOnMouseDragged(MouseDraggedEventHandler);
circle.relocate((e.getX()-10),(e.getY()-10));
canvas.getChildren().add(circle);
System.out.println(e.getSource());
}
}
};
EventHandler<MouseEvent> MouseDraggedEventHandler =
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
double offsetX = t.getSceneX() - orgSceneX;
double offsetY = t.getSceneY() - orgSceneY;
double newTranslateX = orgTranslateX + offsetX;
double newTranslateY = orgTranslateY + offsetY;
((Circle)(t.getSource())).setTranslateX(newTranslateX);
((Circle)(t.getSource())).setTranslateY(newTranslateY);
}
};
}
答案 1 :(得分:0)
您可以在委托父级(窗格)之前在子节点(圆圈)上使用该事件。
EventHandler<MouseEvent> circleOnMousePressedEventHandler = event -> {
orgSceneX = event.getSceneX();
orgSceneY = event.getSceneY();
orgTranslateX = ((Circle) (event.getSource())).getTranslateX();
orgTranslateY = ((Circle) (event.getSource())).getTranslateY();
event.consume();
};