Java Reference
In-Depth Information
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 because window up-
dates are costly in terms of time. If you only need to update a small portion of the window,
it is more efficient to repaint only that region.
An example that demonstrates
repaint( )
is found in
Try This 15-1
.
The update( ) Method
There is another method that relates to repainting called
update( )
that your applet may
want to override. This method is defined by the
Component
class, and it is called when
your applet has requested that a portion of its window be redrawn. The default version of
update( )
simply calls
paint( )
. However, you can override the
update( )
method so that it
performs more subtle repainting, but this is an advanced technique that is beyond the scope
of this topic. Also, overriding
update( )
applies only to AWT-based applets.
Try This 15-1
A Simple Banner Applet
To demonstrate
repaint( )
, a simple banner applet is presented. This applet scrolls a mes-
sage, 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 initial-
ized. Banners are popular Web features, and this project shows how to use a Java applet to
create one.
1.
Create a file called
Banner.java
.
2.
Begin creating the banner applet with the following lines: