How to Make a Timer in Java

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)
Show More

Instructions

    • 1

      Create a timer object: Timer (delay in milliseconds, Action listener). For example:
      private Timer timer1 = new Timer(1500, this);

    • 2

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

    • 3

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

    • 4

      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
      }

    • 5

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

    • 6

      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();
      }
      }

      //imports for TimerExample class:
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.*;
      import javax.swing.*;
      public class TimerExample extends JFrame implements ActionListener {
      private JPanel container;
      JLabel labelCounter;
      private Timer timer1 = new Timer(250, this);
      int w,x,y,z = 1;
      public TimerExample() {
      //set initial delay to 1000 milliseconds
      timer1.setInitialDelay(1150);

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

      //start timer
      timer1.start();
      }
      /**
      * 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();
      g.drawOval(w,x,y,z);
      w = w+2;
      x = x+2;
      y = y+2;
      z = z+2;
      }
      else //stop timer (and drawing) when z coordinate is greater than 99
      timer1.stop();
      }
      }

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Create a Digital Clock in Java

    One of the most powerful features of the Java programming language is its Swing graphical user interface (GUI) library. Java users can...

  • How to Create Your Own Countdown Timer

    Companies and individuals looking to add a countdown timer to their website or social media page have multiple options to create the...

  • How to Make a Timer in Visual Basic

    A timer in Visual Basic will permit the developer to have a predetermined activity happen on his operating system after a certain...

  • How to Delay a Function in Java

    In the Java programming language, a function may need to delay any further processing during a given amount of time. Because different...

  • How to Make a Simple 1-Minute Timer

    Making a one-minute timer is a great project for you to put together with your children. Using a few household items, you...

  • How to Make an Hourglass Sand Timer

    One of the oldest, simplest and most captivating devices invented for the measuring of time, the hourglass remains a popular decoration and...

  • How to Create a Free Countdown Timer

    Creating a free countdown timer for a website or blog is easy. Many social networks allows the use of countdown timers as...

  • How to Create a One Minute Timer

    Creating a one-minute timer is a simple project to help children manage time. One-minute timers can be used for a time out,...

  • How to Create a Java File

    Creating and writing data to a file in Java can be a slightly counter-intuitive task since the creation of the built-in Java...

  • How to Use a Timer in Visual Basic

    The Timer control in Visual Basic is a very useful device that sends a pulse to an application at pre-determined intervals. The...

  • Javascript Timer Tutorial

    Creating a web page that refreshes after every few minutes requires the use of JavaScript. With JavaScript, you can set a page...

  • How to Set a Light Timer

    There are many different varieties of light timers and reasons you may need to have them. There is sometimes a bit of...

  • How to Make a Digital Countdown Timer

    Digital timers use an electronic oscillator, counter circuits and a binary-to-decimal decoder to produce timings of different lengths. The oscillator is tuned...

  • How to Make Java Games

    Java games are computer games written in the computer programming language "Java." Java games can be as complex as the shoot-'em-up three...

  • How to Create a Countdown Timer for my Desktop for Free

    A countdown timer can keep track of the time remaining until a special event or important meeting or until a meal you...

  • Java Tutorial on the Random Method

    Randomly generated numbers have many uses in computer programs, such as creating unpredictability in games, modeling simulations and performing encryption. Java ...

  • How to Make a Timer in Game Maker

    The Game Maker development suite by YoYo Games is popular for its ability to let novice game developers create their dream games...

  • How to Program Digital Timers

    Digital timers are an extra feature that is present in many electronic devices such as microwaves, ovens and cellphones. Due to the...

  • How to Use Java Script to Add Time to Your Web Page

    JavaScript is a programming language that extends a browser's default functionality. Although it can't open or save documents to your hard drive...

Related Ads

Featured