Results 1 to 3 of 3
  1. #1
    JayB is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

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


    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default

    Have you looked at the javax.swing.Timer class?

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    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

  1. Replies: 25
    Last Post: 12-17-2013, 08:12 PM
  2. Urgently need a countdown timer code...
    By BZwap in forum New To Java
    Replies: 5
    Last Post: 05-08-2011, 06:51 AM
  3. writing a countdown using java
    By stevie171 in forum New To Java
    Replies: 7
    Last Post: 12-01-2010, 04:52 PM
  4. countdown timer, little help with method
    By sidy in forum New To Java
    Replies: 22
    Last Post: 07-19-2008, 12:42 PM
  5. CountDown timer
    By Seema Sharma in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •