In: Computer Science
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels, filled with a random color at a random location on the screen. When you click the circle, it disappears and a new random color circle is displayed at another random location (see display below). After twenty circles are clicked, display the time spent in the pane. To detect whether a point is inside the circle, use the contains method defined in the Node class. You can capture the initial starting time with a statement like:
startTime = System.currentTimeMillis()
/*Java program named CircleZapper that displays a circle with a
radius of 10 pixels, filled with a random color at a random
location on the screen. When you click the circle, it disappears
and a new random color circle is displayed at another random
location (see display below). After twenty circles are clicked,
display the time spent in the pane. To detect whether a point is
inside the circle, use the contains method defined in the Node
class. You can capture the initial starting time with a statement
like:
startTime = System.currentTimeMillis()
*/
import ToolKit.StopWatch;
import javafx.application.Application;
import javafx.scene.*;
import javafx.stage.Stage;
public class CircleZapper extends Application {
static int crlCount = 0;
@Override
public void start(Stage primaryStage) {
double paneWidth = 500;
double paneHeight = 500;
// Create an object of circle
Circle crl = new Circle(0, 0, 10);
updateCircle(crl);
Pane pane = new Pane(crl);
Text count = new Text(50,50,crlCount + "");
pane.getChildren().add(count);
StopWatch timer = new StopWatch();
// Create and register the handle
crl.setOnMouseClicked(e-> {
if (!timer.isOn()) {
timer.start();
startTime =
System.currentTimeMillis();
}
if (crlCount < 19) {
crlCount++;
count.setText(crlCount + "");
updateCircle(crl);
} else {
timer.stop();
endTime =
System.currentTimeMillis();
pane.getChildren().remove(crl);
pane.getChildren().add(new Text(paneWidth / 2, paneHeight / 2,
"Total Time spent is " + timer.getElapsedTime() + "
milliseconds"));
pane.getChildren().add(new
Text(paneWidth / 2, paneHeight / 2, "Total Time spent is " +
(startTime - endTime) + " milliseconds"));
}
});
// Create a scene and place it in the stage
primaryStage.setScene(new Scene(pane, paneWidth,
paneHeight));
primaryStage.setTitle("CircleZapper");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
// FUnction to update the circle with randon color and place at
random location
private void updateCircle(Circle c) {
double min = c.getRadius() + 5;
double max = 500 - c.getRadius() - 5;
c.setCenterX((Math.random() * (max - min) + min));
max = 500 - c.getRadius() - 5;
c.setCenterY((Math.random() * (max - min) + min));
c.setFill(new Color(Math.random(), Math.random(), Math.random(),
1));
}
}