Таймер для выполнения анимации
Сделал анимацию. Нужен таймер, в конце в последнем методе он выводит просто время выполнения анимации. Как сделать что код выполнялся в тичении 5 секунд? Столько времени я укажу на работу анимации, столько что б и выполнялось. Буду очень признателен всем кто подскажет!
import acm.graphics.GLabel;
import acm.graphics.GOval;
import acm.graphics.GRect;
import com.shpp.cs.a.graphics.WindowProgram;
import java.awt.*;
/** A class that performs an agrographic animation for 5 seconds. */
public class Assignment3Part6 extends WindowProgram {
/** Constants that specify the size of the window. */
public static final int APPLICATION_WIDTH = 500;
public static final int APPLICATION_HEIGHT = 500;
/** Constanta, sets the execution time of the animation. */
private static final int TIMER_RATE = 4;
/** The number of pixels to shift the label on each cycle */
private static final double DELTA_X = 2.0;
/** The number of milliseconds to pause on each cycle */
private static final int PAUSE_TIME_TEXT = 3;
/** The string to use as the value of the label */
private static final String HEADLINE = "~ FINISH ~";
/** The size of the ball. */
private static final double BALL_SIZE = 50;
/** The size of the rectangle. */
private static final double SIZE_X = 100;
private static final double SIZE_Y = 20;
/** The amount of time to pause between frames (48fps). */
private static final double PAUSE_TIME = 600.0 / 48;
/** The initial horizontal velocity of the ball. */
private static final double HORIZONTAL_VELOCITY = 1.0;
/** Gravitational acceleration. */
private static final double GRAVITY = 0.425;
/** Elasticity. */
private static final double ELASTICITY = 0.7;
/** Color for the oval*/
private static final Color crimson = new Color (170, 0,80);
public void run() {
startAnimation();
}
/** Creates a ball that can be bounced, placing it in the upper-left
corner
of the screen.
@return A ball that can be bounced. */
private GOval makeBall() {
GOval ball = new GOval(32, 0, BALL_SIZE, BALL_SIZE);
ball.setFillColor(crimson);
ball.setFilled(true);
return ball;
}
/** Simulates a bouncing ball.
@param ball The ball to bounce. */
private void bounceBall(GOval ball) {
/** Keep track of speed down (dy is short for "delta-y.") */
double dy = 0;
/** This is repeated forever. Maybe we should change it to assign
when
gently quote on the right side of the screen! */
/** The variable is used to extend the number of iterations at
each step in the animation. */
int count = 0;
while (count++ < 75) {
/** Move the ball to the current speed. */
ball.move(HORIZONTAL_VELOCITY, dy);
/** Gravity extends downward, increasing the acceleration
downward. */
dy += GRAVITY;
/** If the ball falls under the floor, we return it. IS a
difficult case
to pay attention to - if the ball is already scored
floor, we do not return it. */
if (ballBelowFloor(ball) && dy > 0 ) {
dy *= -ELASTICITY;
}
pause(PAUSE_TIME);
}
count = 75;
while(count++ < 175){
ball.move(HORIZONTAL_VELOCITY, dy);
dy += GRAVITY;
if(ballBelowFloorTwo(ball) && dy > 0){
dy *= -ELASTICITY;
}
pause(PAUSE_TIME);
}
count = 175;
while(count++ < 275){
ball.move(HORIZONTAL_VELOCITY, dy);
dy += GRAVITY;
if(ballBelowFloorThree(ball) && dy > 0){
dy *= -ELASTICITY;
}
pause(PAUSE_TIME);
}
while(true){
ball.move(HORIZONTAL_VELOCITY, dy);
dy += GRAVITY;
if(ballBelowFloorFour(ball) && dy > 0){
break;
}
pause(PAUSE_TIME);
}
}
/**
* Determines whether the ball has dropped below floor level.
* @param ball The ball to test.
* @return Whether it's fallen below the floor.
*/
private boolean ballBelowFloor(GOval ball) {
return ball.getY() + ball.getHeight() >= 100;
}
private boolean ballBelowFloorTwo(GOval ball) {
return ball.getY() + ball.getHeight() >= 200;
}
private boolean ballBelowFloorThree(GOval ball) {
return ball.getY() + ball.getHeight() >= 300;
}
private boolean ballBelowFloorFour(GOval ball) {
return ball.getY() + ball.getHeight() >= 400;
}
/** Post the text at the end of the animation. */
private void text(){
GLabel label = new GLabel(HEADLINE);
label.setColor(Color.BLUE);
label.setFont("Algerian-40");
add(label, APPLICATION_WIDTH, 450);
while (label.getX() + APPLICATION_WIDTH > 200) {
label.move(-DELTA_X, 0);
pause(PAUSE_TIME_TEXT);
}
}
/** Creating graphic figures in the form of an obstacle. */
private GRect rect (){
GRect rect = new GRect(10, 100,SIZE_X,SIZE_Y);
rect.setFilled(true);
rect.setFillColor(Color.ORANGE);
return rect;
}
private GRect rectTwo(){
GRect rectTwo = new GRect(110, 200, SIZE_X, SIZE_Y);
rectTwo.setFillColor(Color.ORANGE);
rectTwo.setFilled(true);
return rectTwo;
}
private GRect rectThree(){
GRect rectThree = new GRect(220, 300, SIZE_X, SIZE_Y);
rectThree.setFillColor(Color.ORANGE);
rectThree.setFilled(true);
return rectThree;
}
private GRect rectFinish1(){
GRect rect1 = new GRect(310, 400, SIZE_X, SIZE_Y);
rect1.setFillColor(Color.green);
rect1.setFilled(true);
return rect1;
}
private GRect rectFinish2(){
GRect rect2 = new GRect(310, 380, 20, 40);
rect2.setFillColor(Color.green);
rect2.setFilled(true);
return rect2;
}
private GRect rectFinish3(){
GRect rect3 = new GRect(400, 320, 20, 100);
rect3.setFillColor(Color.green);
rect3.setFilled(true);
return rect3;
}
/** Placing all figures in one method. */
private void addFigureIsRectangle(){
GRect rect = rect();
add(rect);
GRect rectTwo = rectTwo();
add(rectTwo);
GRect rectThree = rectThree();
add(rectThree);
GRect rectFinish1 = rectFinish1();
add(rectFinish1);
GRect rectFinish2 = rectFinish2();
add(rectFinish2);
GRect rectFinish3 = rectFinish3();
add(rectFinish3);
}
/** Timer for animation. */
private void startAnimation(){
long start = System.currentTimeMillis();
addFigureIsRectangle();
GOval ball = makeBall();
add(ball);
bounceBall(ball);
text();
long end = System.currentTimeMillis();
long elapsed = end - start;
long seconds = elapsed / 1000;
println(seconds);
}
}