In: Computer Science
Write a program that draws a fixed circle centered at (100, 60) with radius 50. Whenever the mouse is moved, display a message indicating whether the mouse point is inside the circle at the mouse point or outside of it. Write in Java please.
JAVA CODE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.*;
public class CircleEx extends Application
{
//overriding the start method in the application
class
@Override
public void start(Stage ps)
{
// creating the pane
Pane p = new Pane();
// creating cirle with specified
properties
Circle c = new Circle(100, 60,
50);
c.setFill(Color.CYAN);
c.setStroke(Color.RED);
p.getChildren().add(c);
// registering the handle
p.setOnMouseMoved(mouseevent ->
{
p.getChildren().clear();
Text t = new
Text(mouseevent.getX(), mouseevent.getY(), "Mouse pointer is now "
+
(c.contains(mouseevent.getX(),
mouseevent.getY()) ? "inside " : "outside ") +
"the circle");
p.getChildren().addAll(c, t);
});
// creating the scene
Scene sc = new Scene(p, 350,
150);
//setting the stage title
ps.setTitle("Circle
Exercise");
//placing the scene in the
stage
ps.setScene(sc);
//displaying the stage
ps.show();
}
}
SCREENSHOT FOR OUTPUT: