122 Introduction to Java Applets Chapter 3 to (Web hosting support)
122 Introduction to Java Applets Chapter 3 to output a string containing a newline character (n), you will simply see a small black box at that position in the string. To make drawing more interesting, the applet of Fig. 3.10 draws two lines and a string. The HTML file to load the applet into an applet container is shown in Fig. 3.11. 1 // Fig. 3.10: WelcomeLines.java 2 // Displaying text and lines 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeLines extends JApplet { 11 12 // draw lines and a string on applet s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super.paint( g ); 17 18 // draw horizontal line from (15, 10) to (210, 10) 19 g.drawLine( 15, 10, 210, 10 ); 20 21 // draw horizontal line from (15, 30) to (210, 30) 22 g.drawLine( 15, 30, 210, 30 ); 23 24 // draw String between lines at location (25, 25) 25 g.drawString( “Welcome to Java Programming!”, 25, 25 ); 26 27 } // end method paint 28 29 } // end class WelcomeLines Coordinate (15, 10) Coordinate (15, 30) Coordinate (210, 10) Coordinate (210, 30) Fig. 3.10 Drawing strings and lines. 1 2 4 Fig. 3.11 The WelcomeLines.htmlfile, which loads class WelcomeLinesof Fig. 3.10 into the appletviewer. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01