How to Make a Timer in Java

Save

This tutorial is a guide to using the timer found in the Swing class of the Java programmer language, beginning with an explanation of timer methods and concluding with a complete program that illustrates basic use of the timer object. Timers can be used to specify a future action or for timing dependent or repeated activities such as animation. The unit of time used by the timer object is milliseconds.

Things You'll Need

  • Java Standard Development Kit (SDK)
  • Java Integrated Development Environment (IDE)
  • Create a timer object: Timer (delay in milliseconds, Action listener). For example:
    private Timer timer1 = new Timer(1500, this);

  • An optional initial delay can be set. This delay will occur once after the timer is started. For example:
    timer1.setInitialDelay(5000);

  • Start timer. For example:
    timer1.start();

  • Specify the action to be performed at the timer’s intervals in the actionPerformed() method. For example:
    public void actionPerformed(ActionEvent e) {
    //action to perform code
    }

  • Stop timer. For example:
    timer1.stop();

  • The following code is a simple working example of how to use a timer to create a continuous drawing of ovals that increase in size and descend vertically down the window.

    public class TimerMain {

        //main function instantiates TimerExample object

    public static void main(String[] args){
    TimerExample display = new TimerExample()
    GO
    }
    }

    //imports for TimerExample class:
    import java.awt.event.ActionEvent
    GO
    import java.awt.event.ActionListener
    GO
    import java.awt.
    GO
    import javax.swing.

    GO
    public class TimerExample extends JFrame implements ActionListener {
    private JPanel container
    GO
    JLabel labelCounter
    GO
    private Timer timer1 = new Timer(250, this)
    GO
    int w,x,y,z = 1
    GO
    public TimerExample() {
    //set initial delay to 1000 milliseconds
    timer1.setInitialDelay(1150)
    GO

    //initialize window
    container = new JPanel();
    this.add(container)
    GO
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    GO
    setSize(200,200)
    GO
    this.setVisible(true)
    GO

    //start timer
    timer1.start()
    GO
    }
    /*
    when timer begins this method draws ovals that increase in size
    and descend vertically down the window
    /
    public void actionPerformed(ActionEvent e) {
    if (z < 100){
    Graphics g = container.getGraphics()
    GO
    g.drawOval(w,x,y,z)
    GO
    w = w+2
    GO
    x = x+2
    GO
    y = y+2
    GO
    z = z+2
    GO
    }
    else //stop timer (and drawing) when z coordinate is greater than 99
    timer1.stop()
    GO
    }
    }

References

  • Java Foundation Classes in a Nutshell, Flanagan, David, 1999.
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!