Is there any clause in applet that counts down the remaining time given?

Can i use a swing timer and display the amount time remaining in the game?

Do you believe that I posted that answer and link to the tutorial just because I enjoy suggesting random, irrelevant possibilities?

commented: Agreed :) +3
commented: lolzz ..... :D +4

lol that was funny! The Timer class is definitely the way to go. And since the Timer class uses ActionEvents, the way the count down is displayed is controlled by you (that is, the ActionEvent can be to simply repaint() the updated time).

Hmmm, and what if you wanted an automatic action to be taken at the end of the count down? Would it be easier to create a new Timer that has an update interval of the count down duration - with the appropriate action event set; or would it be better to add an ActionEvent to the existing Timer and perform the check? I didn't read the tutorial so I'm not sure if the answer is there but this sounds so familiar - I feel I've programmed it before... ah but in C# Oops did I just ramble? lol

Oh, 1 last thing, the Timer class can also be found in java.util.Timer just btw

I know i should have mention this from beginning but am a newbie in programming java applet so am using a book "Java How To Program 7ED" to guide me to program a game project so i was wondering if there any resources like tutorial links on how to do a countdown timer for a game i will appreciate it because i barely understand what you guys are talking about xD

Do you believe that I posted that answer and link to the tutorial just because I enjoy suggesting random, irrelevant possibilities?

Well its not like i haven't read the tutorial i even tried the examples but the didn't mention in the tutorial how to view the remaining time i guess you misunderstood me

The amount of time allotted for the game is something you would set in your program. You use the timer to subtract a certain amount of time from your game time when it fires. So if you start with say 60 seconds and use the timer to subtract 1 from that every second (1000ms timer interval), you simply display the remainder in your applet.

Edit: That's not going to give you a fine-grained timer due to scheduling updates on the AWT thread, but it's close enough for your purposes.

Very simple example of this

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TimerExample extends JFrame {

    final JLabel label;
    Timer countdownTimer;
    // Initial game time
    int timeRemaining = 10;

    public TimerExample() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 200);
        label = new JLabel(String.valueOf(timeRemaining), JLabel.CENTER);
        getContentPane().add(label);

        countdownTimer = new Timer(1000, new CountdownTimerListener());
        setVisible(true);
        countdownTimer.start();
    }

    class CountdownTimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (--timeRemaining > 0) {
                label.setText(String.valueOf(timeRemaining));
            } else {
                label.setText("Time's up!");
                countdownTimer.stop();
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TimerExample();
            }
        });
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.