banner



How To Use A Java Timer For Animations

A Swing timer (an instance of javax.swing.Timer (in the API reference documentation)) fires one or more than action events later on a specified delay. Don't confuse Swing timers with the general-purpose timer facility that was added to the coffee.util packet in v 1.3. This page describes only Swing timers; y'all tin can find information on full general-purpose timers in Using the Timer and TimerTask Classes(in the Creating a GUI with JFC/Swing trail). In general, we recommend using Swing timers rather than general-purpose timers for GUI-related tasks. Nonetheless, you might use general-purpose timers if you prefer their API or need their features.

Y'all can apply Swing timers in two means:

  • To perform a task in one case, after a delay.
    For example, the tool tip manager uses Swing timers to determine when to show a tool tip and when to hide it.
  • To perform a chore repeatedly.
    For case, you might perform animation or update a component that displays progress toward a goal. Come across Creating an Animation Loop with Timer(in the Creating a GUI with JFC/Swing trail) for an example and discussion of using a timer for animation.
Notation that the Swing timer'south task is performed in the event-dispatching thread. This means that the chore can safely manipulate components, but it besides means that the task should execute chop-chop. If the task might take a while to execute, and then consider using a SwingWorker instead of or in addition to the timer. See How to Apply Threads for instructions about using the SwingWorker class and data on using Swing components in multi-threaded programs.

Permit'due south look at an case of using a timer to periodically update a component that displays progress toward a goal. Here's a picture of an awarding that uses a timer and a progress bar to display the progress of a long-running task.

An application that uses a timer to periodically update a progress bar

Try this:
  1. Compile and run the application. The main source file is ProgressBarDemo.java (in a .java source file). You will also demand two other source files.
    See Getting Started with Swing if y'all need aid compiling or running this application.
  2. Push the Start button. Watch the progress bar as the job makes progress.

Here's the code from ProgressBarDemo.coffee (in a .java source file) that creates a timer set upward to "become off" every 2d. Each fourth dimension a timer goes off it fires an action event. Conveniently, the constructor also takes an activity listener, which is implemented as an anonymous inner class. The activeness listener contains the code that implements the timer's task.
public final static int ONE_SECOND = 1000; ... timer = new Timer(ONE_SECOND, new ActionListener() {     public void actionPerformed(ActionEvent evt) {            //...Perform a task...            }     });          
When the user presses the Showtime button, the program starts the timer:
timer.commencement();          
When the task is finished, the timer'south action listener stops the timer:
if (/* task is done */) {     ...     timer.stop();     ... }          
The following tables list the commonly used javax.swing.Timer constructors and methods. The API for using Swing timers falls into ii categories:
  • Creating and Initializing the Timer
  • Running the Timer

Creating and Initializing the Timer
Method or Constructor Purpose
Timer(int, ActionListener) Create a Swing timer. The int argument specifies the number of milliseconds to pause between activity events. Use setDelay to modify the delay later construction. The 2nd argument is an action listener, which the constructor registers with the timer. You tin can also register action listeners with addActionListener and remove them with removeActionlistener.
void setDelay(int)
int getDelay()
Set or get the number of milliseconds between action events.
void setInitialDelay(int)
int getInitialDelay()
Prepare or become the number of milliseconds to look before firing the beginning action consequence. By default the initial delay is equal to the regular delay.
void setRepeats(boolean)
boolean isRepeats()
Set or get whether the timer repeats. By default this value is true. Call setRepeats(false) to set a timer that fires a single activity event and then stops.
void setCoalesce(boolean)
boolean isCoalesce()
Set or go whether the timer coalesces multiple, pending action events into a single activity issue. By default this value is true.

Running the Timer
Method Purpose
void start()
void restart()
Plow the timer on. restart as well cancels any pending activeness events.
void stop() Plough the timer off.
boolean isRunning() Go whether the timer is running.

Source: http://fizyka.umk.pl/~jacek/docs/javatutorial/uiswing/misc/timer.html

Posted by: fullertonsulthen.blogspot.com

0 Response to "How To Use A Java Timer For Animations"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel