Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a JavaFX application that allows the user to draw (scribble) on the scene

ID: 3722731 • Letter: W

Question

write a JavaFX application that allows the user to draw (scribble) on the scene by dragging the mouse. In the upper-left corner provide a color picker to choose the current drawing color and a button to clear the scene. Hint: add a new polyline to the scene each time the mouse button is pressed. As the mouse is dragged, add points to the current polyline. Think about the mouse actions that are going to be used (a mouse click will not be sufficient). Do this with out using Jpanel

Explanation / Answer

public class DrawingSample extends Application { public void start(Stage stage) { FlowPane flowPane = new FlowPane(); Canvas canvas = new Canvas(300, 300); flowPane.getChildren().add(canvas); GraphicsContext graphicsContext = canvas.getGraphicsContext2D(); graphicsContext.setFill(Color.WHITE); graphicsContext.fillRect(0, 0, 300, 300); canvas.setOnMouseDragged((event) -> { graphicsContext.setFill(Color.BLACK); graphicsContext.fillRect(event.getX(), event.getY(), 1, 1); }); stage.setScene(new Scene(flowPane)); stage.show(); } public static void main(String[] args) { launch(DrawingSample.class); } }