Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 31 May 2013

JAVA Program: Number Loop (Experimental Program of the Timer class)

Posted on 21:08 by Unknown
Hello there.

Here's another program I want to present to you: it's a self-running experiment that tests the Timer class provided in JAVA. This class fires an action event at specified intervals of time, with a delay in between. You specify the delay you want in the Timer object, then when it comes time to use it, use the methods accordingly.

For this case, we want to test the Timer class for incrementing a value. To show the roll, this is done on a GUI program. You are welcome to modify this as you wish.

How it works is, for every 60 milliseconds, the Timer object fires an action event that upon implementation of the actionPerformed method, the value displayed is incremented with the new value displayed.

Here's the code to this experiment. There is no documentation provided.


import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;

public class NumberLoop extends JFrame
{
    private JPanel panel;
    protected JLabel number;
    private int currentNumber = 0;
    private final int max = 100;
  
    private Timer loopController;
    private final short loopDelayLength = 60; // 40 milliseconds
  
    public static void main(String[] args)
    {
        new NumberLoop();
    }
  
    public NumberLoop()
    {
        setTitle("GUI Number Loop [Experimental]");
        setSize(40,70);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        setLayout(new BorderLayout());
      
        buildPanel();
      
        add(panel, BorderLayout.CENTER);
      
        setVisible(true);
      
        init();
    }
  
    public void init()
    {
        loopController = new Timer(loopDelayLength, new TimerListener());
        loopController.start();
    }
  
    private void buildPanel()
    {
        panel = new JPanel();
      
        number = new JLabel(Integer.toString(currentNumber));
        panel.add(number);
      
    }
  
    private class TimerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            currentNumber++;
            number.setText(Integer.toString(currentNumber));
          
            if (currentNumber == max)
                loopController.stop();
        }
    }
}
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • Java Programming : For Loop Example
    Here's an example of using a for loop in JAVA. This program uses a loop inside another to create a list of integers where the number on ...
  • JAVA Program: Military Time to Real Time
    Hello, people. How you doing? I'm really busy with my college homework, but I am working so hard to pass my courses and continue in my s...
  • Java Program : Fat Gram Calculator
    Here's another program I want to share with you guys, which is simply a program that multiplies the number of grams of fat in food by ni...
  • JAVA Code for the Rock Paper Scissors Program
    Hello, people. How are you doing? I want to show off the code for the game of Rock Paper Scissors where it's just one player against the...
  • JAVA Program: Lottery Class
    Here's a nother JAVA Program I want to share with you guys , which is a demonst ratio n of the Lottery class I w as as ked to design fro...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Video Game Quotes
    Hello, people. How are you doing? I'm good. As I try to build attention and struggle impatiently with getting more YouTube views and hel...

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ▼  May (12)
      • JAVA Program: Number Loop (Experimental Program of...
      • JAVA Program: Displaying Unlimited Strings on a GU...
      • JAVA Program: Command-based String Remover in Text...
      • Arcade Games to check out
      • JAVA Program: Random 0s and 1s Table with Refresh ...
      • JAVA Program: Object-Oriented Programming Class, t...
      • Announcement on "The Asperger Computer"
      • JAVA Program : Hello World!
      • Introducing myself (original blog on Autism Suppor...
      • Demonstration of JAVA Classes
      • Non-fiction e-book, The Asperger Computer, reveals...
      • The Asperger Computer - Facebook Edition
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile