Results 1 to 3 of 3
Thread: Java Countdown Timer
- 05-11-2011, 05:12 PM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
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);
}
}
- 05-12-2011, 03:10 AM #2
Have you looked at the javax.swing.Timer class?
- 05-12-2011, 03:17 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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.
Java 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(); } }Last edited by Solarsonic; 05-12-2011 at 03:43 AM.
Similar Threads
-
Urgently need a countdown timer code...
By BZwap in forum New To JavaReplies: 5Last Post: 05-08-2011, 06:51 AM -
writing a countdown using java
By stevie171 in forum New To JavaReplies: 7Last Post: 12-01-2010, 04:52 PM -
Countdown Timer that does not disrupt normal operation
By colpwd in forum New To JavaReplies: 23Last Post: 08-30-2010, 03:20 AM -
countdown timer, little help with method
By sidy in forum New To JavaReplies: 22Last Post: 07-19-2008, 12:42 PM -
CountDown timer
By Seema Sharma in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks