Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

teacher gave me this to practice and play around with but even tho mine works, s

ID: 3701895 • Letter: T

Question

teacher gave me this to practice and play around with but even tho mine works, sometimes it gets kind of glitchy so i wanted to see how someone else would do it and compare

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()

Explanation / Answer

Below is your code: -

Please let me know in comments if you have any issue.

CircleZapper.java

public class CircleZapper extends Application {

static int circleCount = 0;

@Override

public void start(Stage primaryStage) {

double width = 500;

double height = 500;

Circle c = new Circle(0, 0, 10);

updateCircle(c);

Pane pane = new Pane(c);

StopWatch timer = new StopWatch();

c.setOnMouseClicked(e -> {

if (!timer.isOn()) {

timer.start();

}

if (circleCount < 19) {

circleCount++;

updateCircle(c);

} else {

timer.stop();

pane.getChildren().remove(c);

pane.getChildren().add(

new Text(width / 2, height / 2, "Time spent is " + timer.getElapsedTime() + " milliseconds"));

}

});

primaryStage.setScene(new Scene(pane, width, height));

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));

}

}

StopWatch.java

public class StopWatch {

private long mStartTime;

private long mEndTime;

private long mElapsedPause;

private int mLastSecond = 0;

private boolean mIsOn;

private boolean mIsPaused;

private int mSeconds;

private int mMinutes;

private int mHours;

public StopWatch() {

mStartTime = System.currentTimeMillis();

}

public long getStartTime() {

return mStartTime;

}

public long getEndTime() {

return mEndTime;

}

public void start() {

mIsOn = true;

mStartTime = System.currentTimeMillis();

}

public void stop() {

mEndTime = System.currentTimeMillis();

mIsOn = false;

}

public long getElapsedTime() {

return mEndTime - mStartTime;

}

public long peek() {

return System.currentTimeMillis() - mStartTime;

}

public void pause() {

mIsPaused = true;

mElapsedPause = System.currentTimeMillis() - mStartTime;

}

public void resume() {

mIsPaused = false;

mStartTime = System.currentTimeMillis() - mElapsedPause;

}

public boolean isOn() {

return mIsOn;

}

public boolean nextSecond() {

updateTime();

if (mSeconds != mLastSecond) {

mLastSecond = mSeconds;

return true;

} else {

return false;

}

}

public boolean nextFiveSeconds() {

updateTime();

return mSeconds % 5 == 0;

}

public int getHour() {

updateTime();

return mHours;

}

public int getMinute() {

updateTime();

return mMinutes;

}

public int getSeconds() {

updateTime();

return mSeconds;

}

private void updateTime() {

long currentTime = peek() / 1000;

mSeconds = (int) (currentTime % 60);

currentTime = currentTime / 60;

mMinutes = (int) (currentTime % 60);

currentTime = currentTime / 60;

mHours = (int) (currentTime % 24);

}

@Override

public String toString() {

updateTime();

String hours = getTimeFormat(mHours);

String minutes = getTimeFormat(mMinutes);

String seconds = getTimeFormat(mSeconds);

return hours + ":" + minutes + ":" + seconds;

}

private String getTimeFormat(int time) {

return (time > 9) ? time + "" : "0" + time;

}

public void reset() {

stop();

mHours = 0;

mMinutes = 0;

mSeconds = 0;

mStartTime = 0;

mEndTime = 0;

}

public boolean isPaused() {

return mIsPaused;

}

}