How to get coordinates for the center of Path in javafx?

Evald2

I've got Path element, which is manually drawn and saved as a Node. I cannot figure how to get center coordinates for this entire node, so if relocate my Path using relocate() method and later decide to put it back where it was, it does not return to it's initial position. I have tried using getLayoutX() and getLayoutY() methods, but it moves my node further up from original position.

Code i use for relocate

class PathEventHandler implements EventHandler<MouseEvent>{
//element number in paths
public int n;
public PathEventHandler(int n){
    this.n=n;
}
@Override
public void handle(MouseEvent me) {
    if (me.getEventType() == MouseEvent.MOUSE_ENTERED) {
        paths.get(n).setEffect(new DropShadow(20, Color.BLACK));
    }
    if (me.getEventType() == MouseEvent.MOUSE_EXITED) {
        paths.get(n).setEffect(null);
    }
    if (i == 2) {
    if (me.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        listX.add(paths.get(n).getLayoutX());
        listY.add(paths.get(n).getLayoutY());
        paths.get(n).relocate(me.getSceneX(), me.getSceneY());
        cachePath.add(paths.get(n));
        cacheType.add("Relocate");
    }
    if (me.getEventType() == MouseEvent.MOUSE_CLICKED) {
        if (me.getButton() == MouseButton.SECONDARY) {
        root.getChildren().remove(paths.get(n));
        cachePath.add(paths.get(n));
        cacheType.add("Remove");
        }
    }
    }
}

}
José Pereda

Based on your previous question, and what you've barely described in this one, I've tried to figure out what you are trying to accomplish.

Maybe it's far from there, but this short but functional code should be enough for you to understand how to deal with path creation (as for the first question) and path movement (for the actual question), all within the same mouse listener.

private Path path;
private double x1, y1;

@Override
public void start(Stage primaryStage) {

    AnchorPane root = new AnchorPane();

    root.addEventHandler(MouseEvent.ANY, e -> {
        if(e.getTarget() instanceof Path){
            // Select existing path
            Path path1 = (Path)e.getTarget();

            if (e.getEventType() == MouseEvent.MOUSE_ENTERED_TARGET) {
                path1.setEffect(new DropShadow(20, Color.BLACK));
            } else if (e.getEventType() == MouseEvent.MOUSE_EXITED_TARGET) {
                path1.setEffect(null);
            } else if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
                x1=e.getX(); 
                y1=e.getY();
            } else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                // traslate path
                path1.setTranslateX(e.getX()-x1+path1.getTranslateX());
                path1.setTranslateY(e.getY()-y1+path1.getTranslateY());
                x1=e.getX(); 
                y1=e.getY();
            } else if (e.getButton()==MouseButton.SECONDARY) {
                // right-click over the path to move it to its original position
                path1.setTranslateX(0);
                path1.setTranslateY(0);
            }
        } else {
            // Generate new path
            if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
                path = new Path();
                path.setStroke(Color.BLACK);
                path.setStrokeWidth(10);
                path.getElements().add(new MoveTo(e.getX(), e.getY()));
                root.getChildren().add(path);
            } else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED || 
                e.getEventType() == MouseEvent.MOUSE_RELEASED) {
                path.getElements().add(new LineTo(e.getX(), e.getY()));
            }
        }
    });

    Scene scene = new Scene(root, 600, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

JavaFX - How to calculate the center of a path object?

From Dev

How to get new coordinates of the edited shape and calculate the center dynamically

From Dev

Get the center coordinates of phone screen

From Dev

Get the center coordinates of phone screen

From Dev

How to get file path from JavaFX FileChooser?

From Dev

iOS: How to get route path between two coordinates

From Dev

How to get shortest driving path and distance between two geopoint coordinates?

From Dev

Gimp: How can I get the coordinates of path tool points?

From Dev

How to get path of Bezier curves with coordinates of decimal value

From Dev

iOS: How to get route path between two coordinates

From Dev

how to center an image in javafx

From Dev

Javafx How to set coordinates of a popup?

From Dev

Get Image path JavaFx

From Dev

Get center coordinates from MapKit and display in UILabel

From Dev

Get screen coordinates of a node in javaFX 8

From Dev

JavaFX - get scene coordinates of child in ScrollPane

From Dev

How to get Coordinates of a Sphere?

From Dev

How to get vtkboxwidget coordinates

From Dev

how to center a label on a pane in javafx

From Dev

How to change the Point coordinates of opengl in the center

From Dev

Get absolute coordinates of SVG path with Javascript

From Dev

How to get object coordinates from screen coordinates?

From Dev

How do I get the shortest path between two given coordinates in MapView?

From Dev

How does locationManager get coordinates?

From Dev

How to get coordinates of an svg element?

From Dev

How to get the Coordinates of the Transparency in an Image

From Dev

How to get geo coordinates of road?

From Dev

How to get all the elements' coordinates

From Dev

How to get city from coordinates?

Related Related

  1. 1

    JavaFX - How to calculate the center of a path object?

  2. 2

    How to get new coordinates of the edited shape and calculate the center dynamically

  3. 3

    Get the center coordinates of phone screen

  4. 4

    Get the center coordinates of phone screen

  5. 5

    How to get file path from JavaFX FileChooser?

  6. 6

    iOS: How to get route path between two coordinates

  7. 7

    How to get shortest driving path and distance between two geopoint coordinates?

  8. 8

    Gimp: How can I get the coordinates of path tool points?

  9. 9

    How to get path of Bezier curves with coordinates of decimal value

  10. 10

    iOS: How to get route path between two coordinates

  11. 11

    how to center an image in javafx

  12. 12

    Javafx How to set coordinates of a popup?

  13. 13

    Get Image path JavaFx

  14. 14

    Get center coordinates from MapKit and display in UILabel

  15. 15

    Get screen coordinates of a node in javaFX 8

  16. 16

    JavaFX - get scene coordinates of child in ScrollPane

  17. 17

    How to get Coordinates of a Sphere?

  18. 18

    How to get vtkboxwidget coordinates

  19. 19

    how to center a label on a pane in javafx

  20. 20

    How to change the Point coordinates of opengl in the center

  21. 21

    Get absolute coordinates of SVG path with Javascript

  22. 22

    How to get object coordinates from screen coordinates?

  23. 23

    How do I get the shortest path between two given coordinates in MapView?

  24. 24

    How does locationManager get coordinates?

  25. 25

    How to get coordinates of an svg element?

  26. 26

    How to get the Coordinates of the Transparency in an Image

  27. 27

    How to get geo coordinates of road?

  28. 28

    How to get all the elements' coordinates

  29. 29

    How to get city from coordinates?

HotTag

Archive