15.19 (Game: eye-hand coordination)
Write a program that displays a circle of radius 10 pixels filled
with a random color at a random location on a pane, as shown in
Figure 15.29b. When you click the circle, it disappears and a new
random- color circle is displayed at another random location. After twenty
circles are clicked, display the time spent in the pane, as shown in Figure 15.29c.
Write a program that displays a circle of radius 10 pixels filled
with a random color at a random location on a pane, as shown in
Figure 15.29b. When you click the circle, it disappears and a new
random- color circle is displayed at another random location. After twenty
circles are clicked, display the time spent in the pane, as shown in Figure 15.29c.
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; } }
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 Exercise_19 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); Text count = new Text(50,50,circleCount + ""); pane.getChildren().add(count); StopWatch timer = new StopWatch(); c.setOnMouseClicked(e-> { if (!timer.isOn()) { timer.start(); } if (circleCount < 19) { circleCount++; count.setText(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)); } }
No comments :
Post a Comment