Freehand draw with Path : mouse « JavaFX « Java






Freehand draw with Path

 

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

public class Main extends Application {
  Path path;

  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);

    path = new Path();
    path.setStrokeWidth(1);
    path.setStroke(Color.BLACK);

    scene.setOnMouseClicked(mouseHandler);
    scene.setOnMouseDragged(mouseHandler);
    scene.setOnMouseEntered(mouseHandler);
    scene.setOnMouseExited(mouseHandler);
    scene.setOnMouseMoved(mouseHandler);
    scene.setOnMousePressed(mouseHandler);
    scene.setOnMouseReleased(mouseHandler);

    root.getChildren().add(path);
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent mouseEvent) {
      if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
        path.getElements().clear();
        path.getElements()
            .add(new MoveTo(mouseEvent.getX(), mouseEvent.getY()));
      } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        path.getElements()
            .add(new LineTo(mouseEvent.getX(), mouseEvent.getY()));
      }

    }

  };

}

   
  








Related examples in the same category

1.Listen to all mouse events