Requesting Repainting
As a
general rule, an applet writes to its window only when its paint( ) method is called by the AWT. This raises an interesting
question: How can the applet itself cause its window to be updated when its
information changes? For example, if an applet is displaying a moving banner,
what mechanism does the applet use to update the window each time this banner
scrolls? Remember, one of the fundamental architectural constraints imposed on
an applet is that it must quickly return control to the run-time system. It
cannot create a loop inside paint( ) that
repeatedly scrolls the banner, for example. This would prevent control from passing back to the AWT. Given this
constraint, it may seem that output to your applet’s window will be difficult
at best. Fortunately, this is not the case. Whenever your applet needs to
update the information displayed in its window, it simply calls repaint( ).
The repaint( ) method is defined by the
AWT. It causes the AWT run-time system to execute a call to your applet’s update( ) method, which, in its default
implementation, calls paint( ).
Thus, for another part of your applet to output to its window, simply store the output and then call repaint( ). The AWT will then execute a
call to paint( ), which can display
the stored information. For example, if part of your applet needs to output a
string, it can store this string in a String
variable and then call repaint( ). Inside
paint( ), you will output the string
using drawString( ).
The repaint( ) method has four forms. Let’s
look at each one, in turn. The simplest version of repaint( ) is shown here:
void
repaint( )
This
version causes the entire window to be repainted. The following version
specifies a region that will be repainted:
void
repaint(int left, int top, int width, int height)
Here,
the coordinates of the upper-left corner of the region are specified by left and top, and the width and height of the region are passed in width and height. These dimensions are specified in pixels. You save time by
specifying a region to repaint. Window updates are costly in terms of time. If
you need to update only a small portion of the window, it is more efficient to
repaint only that region.
Calling repaint( ) is essentially a request
that your applet be repainted sometime soon. However, if your system is slow or
busy, update( ) might not be called
immediately. Multiple requests for repainting that occur within a short time
can be collapsed by the AWT in a manner such that update( ) is only called sporadically. This can be a problem in
many situations, 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.
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;
volatile 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();
}
//Entry point for the thread that runs the
banner.
public void run() {
// Redisplay banner
for( ; ; ) {
try { repaint();
Thread.sleep(250);
if(stopFlag)
break;
} catch(InterruptedException
e) {}
}
}
Pause the banner. public void stop() {
stopFlag = true; t = null;
}
Display the banner.
public void paint(Graphics g)
{ char ch;
ch = msg.charAt(0);
msg = msg.substring(1,
msg.length()); msg += ch;
g.drawString(msg, 50, 30);
}
}
Following
is sample output:
Let’s
take a close look at how this applet operates. First, notice that SimpleBanner extends Applet, as expected, but it also
implements Runnable. This is
necessary, since the applet will be creating a second thread of execution that
will be used to scroll the banner. Inside init(
), the foreground and background colors of the applet are set.
After
initialization, the run-time system calls start(
) to start the applet running. Inside start(
), a new thread of execution is created and assigned to the Thread variable t. Then, the boolean variable stopFlag, which controls the execution of the applet, is set to false.
Next,
the thread is started by a call to t.start(
). Remember that t.start( )
calls a method defined by Thread,
which causes run( ) to begin
executing. It does not cause a call to the version of start( ) defined by Applet.
These are two separate methods.
Inside run( ), a call to repaint( ) is made. This eventually causes the paint( ) method to be called, and the rotated contents of msg are displayed. Between each
iteration, run( ) sleeps for a
quarter of a second. The net effect is that the contents of msg are scrolled right to left in a
constantly moving display. The stopFlag
variable is checked on each iteration. When it is true, the run( ) method
terminates.
If a
browser is displaying the applet when a new page is viewed, the stop( ) method is called, which sets stopFlag to true, causing run( ) to
terminate. This is the mechanism used to stop the thread when its page is no
longer in view. When the applet is brought back into view, start( ) is once again called, which starts a new thread to execute
the banner.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.