This is a very simple timer and I cant seem to get it to work

I have tried this () runs but does nothing:

// The "Timer" class.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Timer extends Applet implements ActionListener
{
    // Place instance variables here
    Timer timer;
    int num = 0;
    public void init ()
    {

        timer = new Timer ();
       
        timer.start ();

        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {
    g.drawString ("Number: " + num, 100,100);
        // Place the body of the drawing method here
    } // paint method
    
    public void actionPerformed(ActionEvent e) {
    //If still loading, can't animate.
        num += 1;
    }

} // Timer class

and this (gives me an error when I am making the timer):

// The "Timer" class.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Timer extends Applet implements ActionListener
{
    // Place instance variables here
    Timer timer;
    int num = 0;
    public void init ()
    {

        timer = new Timer (1000, this);
       
        timer.start ();

        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {
    g.drawString ("Number: " + num, 100,100);
        // Place the body of the drawing method here
    } // paint method
    
    public void actionPerformed(ActionEvent e) {
    //If still loading, can't animate.
        num += 1;
    }

} // Timer class

Thanks for any help.

Dani AI

Generated

A short, practical summary that ties the thread together and fills in missing details.

The original compile problem was a name clash: declaring the applet class as Timer hides the Swing Timer type, so a call that looks like "new Timer(1000, this)" makes the compiler look for a constructor on the applet class instead of javax.swing.Timer. That is the root cause pointed out; the easiest fixes are to rename the applet class (done by ) or explicitly use the fully qualified Swing type when instantiating the timer.

Applet lifecycle and where to start/stop the timer matter. Starting a repeating GUI timer in start() and stopping it in stop() ensures it runs only while the applet is active; calling start() too early (only in init()) can produce odd behavior. Also call repaint() from the timer's ActionListener so the screen updates after the counter changes.

Two compatibility and threading notes that were not fully explained in-thread: (1) the @Override token caused an error because older Java versions (pre-5) do not support that annotation; removing it or compiling with Java 5+ resolves the error. (2) Prefer javax.swing.Timer for Swing-based animation because it fires on the Event Dispatch Thread and is safe to update GUI state; java.util.Timer runs tasks on a background thread and requires marshaling updates back to the EDT.

Quick checklist:

  • Avoid naming classes the same as JDK classes.
  • If not renaming, disambiguate with javax.swing.Timer.
  • Start the timer in start() and stop it in stop().
  • Call repaint() from the ActionListener.
  • Remove @Override when compiling with Java 1.4 or upgrade the JDK.
  • For Swing UI, consider using JApplet/JPanel and paintComponent rather than mixing heavy AWT Applet with Swing components.

This ties back to 's diagnosis and 's final working approach while adding lifecycle, threading, and JDK-compatibility clarifications.

Recommended Answers

All 12 Replies

This because of the name clash.
Your class name has the same name of a class used in swing package you already use in your program.

i.e.
Your class name is Timer.
In line 10:
you define an instance variable "timer" of type Timer.

the compiler understands that you want to define this variable as an instance of your class!!
it searches for the constructor Timer(int, Timer)
obviously you didn't define this constructor.
a compile error is generated

I hope this description of the problem is understandable.

The solution is simply ... change the class name and every thing will be OK.

I tried changing the name and I still get the error when I have the delay in the bracket. Again when it is blank it works. here is the new code.

// The "Timer" class.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Timer extends Applet implements ActionListener
{
    // Place instance variables here
    Timer adder;
    int num = 0;
    public void init ()
    {

        adder = new Timer (1000,this);
        
        adder.start ();

        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {
    g.drawString ("Number: " + num, 100,100);
        // Place the body of the drawing method here
    } // paint method
    
    public void actionPerformed(ActionEvent e) {
    //If still loading, can't animate.
        num += 1;
        repaint();
    }

} // Timer class

What is the error message you get?

it works.
here is my code which work.

// The "Timer" class.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class method extends Applet implements ActionListener
{
    // Place instance variables here
    Timer timer;
    int num = 0;
    public void init ()
    {

        timer = new Timer (1000, this);

        timer.start ();

        // Place the body of the initialization method here
    } // init method


    @Override
    public void paint (Graphics g)
    {
    g.drawString ("Number: " + num, 100,100);
        // Place the body of the drawing method here
    } // paint method

    public void actionPerformed(ActionEvent e) {
    //If still loading, can't animate.
        num += 1;
        repaint();
    }

} // Timer class

It didn't work the @override was an illegal token and the error I get when I take it out is no applicable overload was found for a constructor of type "Timer". Perhaps you wanted the overloaded version "Timer ()" instead?

Thanks for the help.

well did you check line 7 in my code ?

public class [B][U]method[/U][/B] extends Applet implements ActionListener

check the new class name!!

I copied your code exactly when I tried it and it still doesn't work.

what IDE do you use?

ready to program. Crap I know but thats the one i learned to use and it is simple. its java 1.4 btw.

but I also have netbeans and eclipse

I never use this IDE but try to make a new project with a new class with the new name.
I use netbeans and the code is working.

good luck

Thanks. I got it working I needed a start method here is the code.

// The "Timer" class.
import java.applet.*;
import java.awt.*;
import javax.swing.Timer;
import java.awt.event.*;

public class method extends Applet implements ActionListener
{
    // Place instance variables here
    Timer time;
    int num = 0;
    public void init ()
    {

        time = new Timer (1000, this);
        //time.start ();

        // Place the body of the initialization method here
    } // init method


    public void start ()
    {

        // Start the timer

        time.start ();

    }


    //@Override
    public void paint (Graphics g)
    {
        g.drawString ("Number: " + num, 100, 100);
        // Place the body of the drawing method here
    } // paint method


    public void actionPerformed (ActionEvent e)
    {
        //If still loading, can't animate.
        num += 1;
        repaint ();
    }
} // Timer class

well done

Is this thread solved now sirlink99?

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.