Wednesday, 4 September 2013

Playing with virtual magnets on a virtual whiteboard

public class JavaFXAssessDecideDo extends Application {
   
    @Override
    public void start(Stage primaryStage) {
       
        Circle magnet = createCircle(50,50,Color.RED);
       
        StackPane root = new StackPane();
        root.getChildren().add(magnet);
       
        Scene scene = new Scene(root, 300, 250);
       
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
       
}
   
    private Circle createCircle(double x, double y, Color color) {
    final Circle c = new Circle(x, y, 25);
    c.setFill(color);

    c.setOnMouseDragged(new EventHandler() {

        @Override
        public void handle(MouseEvent event) {
            c.relocate(event.getSceneX() - c.getRadius(), event.getSceneY() - c.getRadius());
        }
    });

    return c;
}




References

http://stackoverflow.com/questions/10530829/javafx-drag-and-drop-moving-icon