How To Use A Java Timer For Animations
A Swing timer (an instance of
javax.swing.Timer
) 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 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 for an example and discussion of using a timer for animation.
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.
Here's the code from
Try this:
- Compile and run the application. The main source file is
ProgressBarDemo.java
. You will also demand two other source files.
See Getting Started with Swing if y'all need aid compiling or running this application.- Push the Start button. Watch the progress bar as the job makes progress.
ProgressBarDemo.coffee
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. When the user presses the Showtime button, the program starts the timer:public final static int ONE_SECOND = 1000; ... timer = new Timer(ONE_SECOND, new ActionListener() { public void actionPerformed(ActionEvent evt) { //...Perform a task... } });
When the task is finished, the timer'south action listener stops the timer:timer.commencement();
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
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) | Set or get the number of milliseconds between action events. |
void setInitialDelay(int) | 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) | 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) | Set or go whether the timer coalesces multiple, pending action events into a single activity issue. By default this value is true. |
Method | Purpose |
---|---|
void start() | 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