Write a JavaFX program named CircleZapper that displays a circle with a radius o
ID: 3836976 • Letter: W
Question
Write a JavaFX 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()
Circle ZapperExplanation / Answer
import ToolKit.StopWatch;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Crcl extends Application {
static int CrclCnt = 0;
startTime = System.currentTimeMillis()
@Override
public void start(Stage primaryStage) {
double wdh = 500;
double hgt = 500;
Circle c = new Circle(0, 0, 10);
updateCircle(c);
Pane pane = new Pane(c);
Text count = new Text(50,50,CrclCnt + "");
pane.getChildren().add(count);
StopWatch timer = new StopWatch();
c.setOnMouseClicked(e-> {
if (!timer.isOn()) {
timer.start();
}
if (CrclCnt < 19) {
CrclCnt++;
count.setText(CrclCnt + "");
updateCircle(c);
} else {
timer.stop();
pane.getChildren().remove(c);
pane.getChildren().add(new Text(wdh / 2, hgt / 2, "Time spent is " +
timer.getElapsedTime() + " milliseconds"));
}
});
primaryStage.setScene(new Scene(pane, wdh, hgt));
primaryStage.setTitle("Game: eye-hand coordination");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
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));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.