-
Java Countdown Timer
can anyone assist in helping me come up with a timer that will countdown and stop my game. I built a simple Click the Dot game as was able to display the number of clicks and the number of successful clicks but I cannot figure out how to put in a timer that will count down from lets say 10 seconds then stop the entire game... I have looked at the timer class.. Any ideas?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
public class Main extends Applet implements Runnable, MouseListener
{
Image dbImage;
Graphics dbGraphics;
int x, y, xspeed, yspeed, radius, count, score;
Thread th = new Thread(this);
boolean going = true;
Random rand = new Random();
public void init()
{
x = getSize().width/2;
y = getSize().height/2;
xspeed = 2;
yspeed = 2;
radius = 30;
count = 0;
score = 0;
addMouseListener(this);
}
public void mousePressed(MouseEvent e){
Rectangle2D.Double mouse_bounds = new Rectangle2D.Double(e.getX()-1,e.getY()-1,2,2);
Ellipse2D.Double ball_bounds = new Ellipse2D.Double(x-radius,y-radius,radius*2,radius*2);
if(ball_bounds.intersects(mouse_bounds))
{
x = rand.nextInt(getSize().width);
y = rand.nextInt(getSize().height);
if(xspeed<0)
xspeed -= 1;
if(yspeed>0)
xspeed+=1;
if(yspeed<0)
yspeed -= 1;
if(yspeed>0)
yspeed += 1;
score+= 1;
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
count+= 1;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public synchronized int getScore() {
return score;
}
public synchronized void increaseScore() {
score++;
}
public synchronized int getCount() {
return count;
}
public synchronized void increaseCount() {
count++;
}
public void start()
{
th.start();
}
public void stop()
{
going = false;
}
public void destory()
{
going = false;
}
public void run()
{
while(going)
{
x += xspeed;
y += yspeed;
if(x<0)
xspeed = Math.abs(xspeed);
if(x>getSize().width)
xspeed = -Math.abs(xspeed);
if(y<0)
yspeed = Math.abs(yspeed);
if(y>getSize().height)
yspeed = -Math.abs(yspeed);
repaint();
try
{
Thread.sleep(20);
}
catch(InterruptedException ie){
}
}
for(int i = 30; i >= 0; i -= 1){
if(i == 0) System.exit(0);
try{
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
public void update(Graphics g)
{
if(dbImage == null)
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbGraphics = dbImage.getGraphics();
}
dbGraphics.setColor(this.getBackground());
dbGraphics.fillRect(0,0,this.getSize().width,this. getSize().height);
dbGraphics.setColor(this.getForeground());
paint(dbGraphics);
g.drawImage(dbImage,0,0,this);
}
public void paint(Graphics g)
{
g.setColor(Color.BLUE);
g.fillOval(x-radius,y-radius,radius*2,radius*2);
g.drawString("Total Clicks: " + getCount(), 20, 50);
g.drawString("Score: "+ getScore(), 20, 60);
}
}
-
Have you looked at the javax.swing.Timer class?
-
Use the Timer class, ActionListener class, and a Thread. Here's a skeleton for you; there's some stuff missing for you to fill out.
Code:
import java.util.Timer;
import java.awt.ActionListener;
import java.awt.ActionEvent;
public class Timer {
ActionListener timerListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
}};
Timer countdownTimer = new Timer(1000, timerListener);
TimerThread t1 = new TimerThread();
t1.start();
public void countdown() {
}
}
class TimerThread extends Thread {
public void run() {
timer.countdown();
}
}