Question
Write a java fix program that displays a green circle and when the mouse is clicked within the application, the circle is moved to that
Write a java fix program that displays a green circle and when the mouse is clicked within the application, the circle is moved to that position in the pane.
A. Write the code that will register the ClickHandler class for click events on pane p.
B. Write the code for the ClickHandler class that will move the circle circ to the position of the mouse click.
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class CircleMove extends Application { private Circle circ; @Override public void start(Stage primaryStage) throws Exception { Pane p = new Pane(); circ = new Circle( 50, 50, 20, Color.FORESTGREEN); p.getChildren().add(circ); // *A* Register the click handler to handle click events on the pane p Scene sc = new Scene(p, 300, 300); primaryStage.setScene(sc); primaryStage.setTitle("Moving Circle"); primaryStage.show(); } private class ClickHandler implements EventHandler
public static void main(String[] args) { launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started