I have this:
Circle circle = null;
List<Circle> circles = new ArrayList<>();
for(List row: list) //list is list of coordinates i.e. [[200,100],[10,5.5],[15,100],[200,25]...]
{
circle = new Circle((double) row.get(0), (double) row.get(1), 4f);
circle.setFill(Color.BLUE);
Tooltip toolTipx = new Tooltip("The point is : " + (double) row.get(0));
Tooltip.install(circle, toolTipx);
circles.add(circle);
}
System.out.println(circles.size());
Pane pane = new Pane();
pane.getChildren().addAll(circles);
This plots the said points on the window perfectly and the tooltip depicts the coordinate of the point.
Now, what I want is to create the plotted points draggable. So that I can drag the points anywhere in the window and the new location(coordinates of the dragged points) is stored to show in the tooltip or in a fixed Label at the end of the window.
I came across this (makeDraggable()), but couldn't find a starting point for me.
How can I achieve this? Thanks.
Used :
circle.setOnMouseDragged( event -> {
circle.setCenterX(event.getX());
circle.setCenterY(event.getY());
});
to trigger the mouseDragging event on any created circles/points.
Then, simply reset the centerX and centerY with event.getX()
(the destination X point) and event.getY()
(the destination Y point).
My Full Code :
public void start(Stage stage) throws IOException
{
Label b = new Label("The coordinates are : ");
b.setLayoutX(190);
b.setLayoutY(280);
Label value = new Label();
value.setLayoutX(320);
value.setLayoutY(280);
List<Circle> circles = new ArrayList<>();
for(List row: list)
{
Circle circle = new Circle((double) row.get(0), (double) row.get(1), 5f);
circle.setFill(Color.BLUE);
//this did the trick for me
circle.setOnMouseDragged( event -> {
circle.setCenterX(event.getX());
circle.setCenterY(event.getY());
});
//for printing the values in a Label
circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
System.out.println("Clicked");
value.setText(circle.getCenterX()+" , "+circle.getCenterY());
}
});
circles.add(circle);
}
System.out.println(circles.size());
Pane pane = new Pane();
pane.getChildren().addAll(circles);
pane.getChildren().add(b);
pane.getChildren().add(value);
Scene scene = new Scene(pane, 600, 300);
//Setting title to the Stage
stage.setTitle("Show Points");
//Adding scene to the stage
stage.setScene(scene);
scene.getStylesheets().add(Main.class.getResource("application.css").toExternalForm());
//Displaying the contents of the stage
stage.show();
} //end of start
public static void main(String[] args) throws IOException {
launch(args);
}