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");
}
}
}
}
}
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.
Comments