img
including animation, in which a consistent update time is necessary. One solution to this
problem is to use the following forms of repaint( ):
void repaint(long maxDelay)
void repaint(long maxDelay, int x, int y, int width, int height)
Here, maxDelay specifies the maximum number of milliseconds that can elapse before update( )
is called. Beware, though. If the time elapses before update( ) can be called, it isn't called. There's
no return value or exception thrown, so you must be careful.
NOTE  It is possible for a method other than paint( ) or update( ) to output to an applet's window.
OTE
To do so, it must obtain a graphics context by calling getGraphics( ) (defined by Component)
and then use this context to output to the window. However, for most applications, it is better and
easier to route window output through paint( ) and to call repaint( ) when the contents of the
window change.
A Simple Banner Applet
To demonstrate repaint( ), a simple banner applet is developed. This applet scrolls a message,
from right to left, across the applet's window. Since the scrolling of the message is a repetitive
task, it is performed by a separate thread, created by the applet when it is initialized. The
banner applet is shown here:
/* A simple banner applet.
This applet creates a thread that scrolls
the message contained in msg right to left
across the applet's window.
*/
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleBanner" width=300 height=50>
</applet>
*/
public class SimpleBanner extends Applet implements Runnable {
String msg = " A Simple Moving Banner.";
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
}
// Start thread
public void start() {
t = new Thread(this);
stopFlag = false;
t.start();
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home