Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA FX When the user stops drawing (releases the mouse button), the canvas should draw a straight line from the endpoint of the drawn shape

JAVA FX

When the user stops drawing (releases the mouse button), the canvas should draw a straight line from the endpoint of the drawn shape to the starting point. Also, you should implement the "eraser" function: the mouse's secondary button (the right button for right-handed people) should paint the canvas white, thus "erasing" the content under the cursor. (using strokeLine() and clearRect() in GraphicsContext interface

Based on the following code:

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.stage.Stage;

public class MainApp1 extends Application {

@Override public void start(Stage stage) throws Exception { Canvas drawCanvas = new Canvas(1000,800); BorderPane root = new BorderPane(); root.setCenter(drawCanvas);

GraphicsContext gc = drawCanvas.getGraphicsContext2D();

//add mouse moved event drawCanvas.setOnMouseDragged( e -> { gc.setFill(Color.WHITE); gc.fillRect(0, 0, 1000, 800); gc.setLineWidth(5); gc.lineTo(e.getX(), e.getY()); gc.stroke(); });

drawCanvas.setOnMouseMoved( e -> { gc.moveTo(e.getX(), e.getY()); });

stage.setScene(new Scene(root)); stage.show();

}

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

What is a regional subculture? Give some examples.

Answered: 1 week ago

Question

What is a product/service feasibility analysis?

Answered: 1 week ago

Question

What is Aufbau's rule explain with example?

Answered: 1 week ago

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago