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.

Calling repaint() forces the paint() method to be called. This statement tells the GUI that something has happened to make a display update necessary.

The LinkRotator applet has a short paint() method:

public void paint(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 screen2D object that represents the drawable area of the applet window. All drawing is done by calling the methods of this object.

The setColor() method of Graphics2D selects the color used for subsequent 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 pageTitle array. The second line displayed is the address of the URL object, which is stored in the pageLink array. The current variable determines the element of the arrays to display.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.24.74