Question
Compile and run the class ClickDistance whose code is below. This is a JavaFx Application that, when you click on it, will display the distance
Compile and run the class ClickDistance whose code is below. This is a JavaFx Application that, when you click on it, will display the distance of the mouse click from the (0, 0) point, i.e., the upper left corner, in pixels.
Once you get ClickDistance running, change the code to use a method
double distanceFromOrigin(int x, int y)
instead of calculating it inside the handleMouseClick method (The actual formula should be the same: (x2+y2)). Note that the "origin" is the point (0,0), i.e. the point where x is 0 and y is 0, i.e., the upper left corner of the Stage.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.text.Text; import javafx.stage.Stage; /************************************************************************ // ClickDistance.java Author: Lewis/Loftus // // Demonstrates the handling of a mouse click event. //************************************************************************/ public class ClickDistance extends Application { private Line line; private Text distanceText; /**-------------------------------------------------------------------- // Shows the distance between the origin (0, 0) and the point where // the mouse is clicked. //--------------------------------------------------------------------*/ public void start(Stage primaryStage) { line = new Line(0, 0, 0, 0); distanceText = new Text(150, 30, "Distance: --"); Group root = new Group(distanceText, line); Scene scene = new Scene(root, 400, 300, Color.LIGHTYELLOW); scene.setOnMouseClicked(this::handleMouseClick); primaryStage.setTitle("Click Distance"); primaryStage.setScene(scene); primaryStage.show(); } /**-------------------------------------------------------------------- // Changes the end point of the line to the location of the mouse // click event and updates the distance from (0,0) displayed. //--------------------------------------------------------------------*/ public void handleMouseClick(MouseEvent event) { double clickX = event.getX(); double clickY = event.getY(); line.setEndX(clickX); line.setEndY(clickY); double distance = Math.sqrt(clickX * clickX + clickY * clickY); String distanceStr = String.format("%.2f", distance); distanceText.setText("Distance: " + distanceStr); } public static void main(String[] args) { launch(args); } }
Now, copy ClickDistance and modify the copy to create a similar class called MouseDistance. MouseDistance will do the same thing as ClickDistance, except it does not need the user to click--it continuously updates the distance as the user moves the mouse. Hint: very little change to the original class is required! Hint 2: it uses a different event type.
Create a JavaFx application that will respond to mouse movements as follows: like MouseDistance it will react whenever the user moves the mouse. It will look at what percentage the x value is of the width of the Scene. It will use that percentage to set the hue of the background color of the scene, as a percentage of the maximum hue (using Color.hsb(double hue, double saturation, double brightness)). Use a fixed saturation and brightness of 1.0.
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