Chapter 3 Introduction to Java Applets 119 1 (Web file server)

Chapter 3 Introduction to Java Applets 119 1 2 3 4 Fig. 3.7 WelcomeApplet.htmlloads class WelcomeAppletof Fig. 3.6 into the appletviewer. Good Programming Practice 3.2 Always test a Java applet in the appletviewer and ensure that it is executing correctly before loading the applet into a World Wide Web browser. Browsers often save a copy of an applet in memory until the current browsing session terminates (i.e., all browser windows are closed). Thus, if you change an applet, recompile the applet, then reload the applet in the browser, you may not see the changes because the browser may still be executing the original version of the applet. Close all your browser windows to remove the old version of the applet from memory. Open a new browser window and load the applet to see your changes. Software Engineering Observation 3.1 If your World Wide Web browser does not support Java 2, most of the applets in this book will not execute in your browser. This is because most of the applets in this book use features that are specific to Java 2 or are not provided by browsers that support Java 1.1. Section 3.6.2 discusses how to use the Java Plug-in to view applets in Web browsers that do not support Java 2. Many HTML codes (or tags) come in pairs. For example, lines 1 and 4 of Fig. 3.7 indicate the beginning and the end, respectively, of the HTML tags in the file. All HTML tags begin with a left angle bracket, <, and end with a right angle bracket, >. Lines 2 and 3 are special HTML tags for Java applets. They tell the applet container to load a specific applet and define the size of the applet s display area (its width and height in pixels) in the appletviewer(or browser). Normally, the applet and its corresponding HTML file are stored in the same directory on disk. Typically, a browser loads an HTML file from a computer (other than your own) connected to the Internet. However, HTML files also can reside on your computer (as we demonstrated in Section 3.2). When an applet container encounters an HTML file that specifies an applet to execute, the applet container automatically loads the applet s .classfile (or files) from the same directory on the computer in which the HTML file resides. The tag has several attributes. The first attribute of the tag on line 2 (code = “WelcomeApplet.class”) indicates that the file WelcomeApplet.class contains the compiled applet class. Remember, when you compile your Java programs, every class is compiled into a separate file that has the same name as the class and ends with the .class extension. The second and third attributes of the tag indicate the width and the height of the applet in pixels. The upper-left corner of the applet s display area is always at x-coordinate 0 and y-coordinate 0. The width of this applet is 300 pixels and its height is 45 pixels. You may want (or need) to use larger width and height values to define a larger drawing area for your applets. The tag (line 3) terminates the tag that began on line 2. The tag (line 4) specifies the end of the HTML tags that began on line 1 with . Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply