ptg7068951
Handling Screen Updates in the paint()Method 273
URL getURL(String urlText) {
URL pageURL = null;
try {
pageURL = new URL(getDocumentBase(), urlText);
} catch(MalformedURLException m) {
// do nothing
}
returnpageURL;
}
The try-catchblock deals with any MalformedURLExceptionerrors that
occur when URLobjects are created. Because nothing needs to happen if this
exception is thrown, the catchblock only contains a comment.
Handling Screen Updates in the
paint() Method
An applet’s paint()method is executed when the applet window needs to be
updated. You can also manually call the paint()method within an applet.
Callingrepaint()forces the paint()method to be called. This statement tells
the GUI that something has happened to make a display update necessary.
The LinkRotatorapplet has a short paint()method:
public voidpaint(Graphics screen) {
Graphics2D screen2D = (Graphics2D) screen;
screen2D.setColor(butterscotch);
screen2D.fillRect(0, 0, getSize().width, getSize().height);
screen2D.setColor(Color.black);
screen2D.drawString(pageTitle[current], 5, 60);
screen2D.drawString(“” + pageLink[current], 5, 80);
}
The first statement in this method creates a screen2Dobject that represents
the drawable area of the applet window. All drawing is done by calling the
methods of this object.
The setColor()method of Graphics2Dselects the color used for subse-
quent drawing. The color is set to butterscotch before a rectangle that fills
the entire applet window is drawn. Next, the color is set to black and lines
of text are displayed on the screen at the (x,y) positions of (5,60) and (5,80).
The first line displayed is an element of the pageTitlearray. The second
line displayed is the address of the URLobject, which is stored in the
pageLinkarray. The currentvariable determines the element of the arrays
to display.