Sams Teach Yourself C in 21 Days

(singke) #1
8: public void paint(Graphics screen) {
9: screen.setFont(f);
10: if (useRed)
11: screen.setColor(Color.red);
12: else
13: screen.setColor(Color.blue);
14: screen.drawString(“This is an applet!”, 5, 30);
15: }
16:
17: public boolean mouseDown(Event evt, int x, int y) {
18: useRed = !useRed;
19: repaint();
20: return true;
21: }
22: }

(See Figure B6.4.)

762 Bonus Day 6

LISTINGB6.9 continued

OUTPUT

FIGUREB6.4
The AppletTest applet
running in Microsoft
Internet Explorer.

Lines 1 and 2 import the required classes, which in this case are the Appletclass
and the AWT classes. Line 4 defines the application as a subclass of Applet.
Line 5 creates a new Fontobject with Times Roman font in 36 point boldface. Line 6
sets up a type Booleanwhich will control the color used for the text display.
Lines 8 through 15 are the paintmethod, which is automatically called by the Applet
superclass as needed. Line 9 specifies that the font created in line 5 will be used for the
text display. Line 10 tests the value of the useRedflag. If it is true, in line 11, the
screen’s color is set to red. If it is false, line 13 sets the screen’s color to blue. Finally
line 14 uses the drawstringmethod to display the text.

ANALYSIS

41 448201x-Bonus6 8/13/02 11:23 AM Page 762

Free download pdf