Kids web site - 114 Introduction to Java Applets Chapter 3 17

114 Introduction to Java Applets Chapter 3 17 18 // draw a String at x-coordinate 25 and y-coordinate 25 19 g.drawString( “Welcome to Java Programming!”, 25, 25 ); 20 21 } // end method paint 22 23 } // end class WelcomeApplet x-axis Pixel coordinate (25, 25), where the string is displayed. y-axis appletviewer window Applet menu The status bar mimics Upper-left corner of what would be drawing area is location displayed in the (0, 0). Drawing area ends browser s status bar as just above the status bar. X-coordinates increase the applet loads and begins executing. from left to right. Y-coordinates increase from top to bottom. Fig. 3.6A first applet in Java and the applet s screen output (part 2 of 2). Fig. 3. This program illustrates several important Java features. We consider each line of the program in detail. Line 19 does the real work of the program, namely drawing the string Welcome toJava Programming! on the screen. But let us consider each line of the program in order. Lines 1 2 // Fig. 3.6: WelcomeApplet.java // A first applet in Java. begin with //, indicating that the remainder of each line is a comment. The comment on line 1 indicates the figure number and file name for the applet source code. The comment on line 2 simply describes the purpose of the program. As stated in Chapter 2, Java contains many predefined components called classes that are grouped into packages in the Java API. Line 5 import java.awt.Graphics; // import class Graphics is an import statement that tells the compiler load class Graphics from package java.awt for use in this Java applet. Class Graphicsenables a Java applet to draw graphics such as lines, rectangles, ovals and strings of characters. Later in the book, you will see that class Graphicsalso enables applications to draw. Line 8 import javax.swing.JApplet; // import class JApplet is an import statement that tells the compiler load class JApplet from package javax.swing. When you create an applet in Java, you normally import the JAppletclass. [Note: There is an older class called Applet from package java.applet that is not used with Java s newest GUI components from the javax.swingpackage. In this book, we use only class JApplet with applets.] Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply