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.

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.