Web hosting domain names - 116 Introduction to Java Applets Chapter 3 When

116 Introduction to Java Applets Chapter 3 When an applet container (the appletviewer or browser in which the applet executes) loads our WelcomeApplet class, the applet container creates an object (instance) of class WelcomeApplet that implements the applet s attributes and behaviors. [Note: The terms instance and object are often used interchangeably.] Applet containers can create only objects of classes that are public and extend JApplet. Thus, applet containers require applet class definitions to begin with the keyword public (line 10). Otherwise, the applet container cannot load and execute the applet. The public keyword and related keywords (such as private and protected) are discussed in detail in Chapter 8, Object-Based Programming. For now, we ask you simply to start all class definitions with the public keyword until the discussion of public in Chapter 8. When you save a public class in a file, the file name must be the class name followed by the .java file name extension. For our applet, the file name must be WelcomeApplet.java. Please note that the class name part of the file name must use the same spelling as the class name, including identical use of uppercase and lowercase letters. For reinforcement, we repeat two Common Programming Errors from Chapter 2. Common Programming Error 3.1 It is an error for a public class if the file name is not identical to the class name (plus the .java extension) in both spelling and capitalization. Therefore, it is also an error for a file to contain two or more public classes. Common Programming Error 3.2 It is an error not to end a file name with the .java extension for a file containing an application s class definition. The Java compiler will not be able to compile the class definition. Testing and Debugging Tip 3.4 The compiler error message Public class ClassName must be defined in a file called Class- Name.java indicates either that the file name does not exactly match the name of the public class in the file (including all uppercase and lowercase letters) or that you typed the class name incorrectly when compiling the class (the name must be spelled with the proper uppercase and lowercase letters). Line 13 public void paint( Graphics g ) begins the definition of the applet s paint method one of three methods (behaviors) that the applet container calls for an applet when the container begins executing the applet. In order, these three methods are init (discussed later in this chapter), start (discussed in Chapter 6) and paint. Your applet class gets a free version of each of these methods from class JApplet when you specify extendsJApplet in the first line of your applet s class definition. If you do not define these methods in your own applet, the applet container calls the versions inherited from JApplet. The inherited versions of methods init and start have empty bodies (i.e., their bodies do not contain statements, so they do not perform a task) and the inherited version of method paint does not draw anything on the applet. [Note: There are several other methods that an applet container calls during an applet s execution. We discuss these methods in Chapter 6, Methods. ] To enable our applet to draw, class WelcomeApplet overrides (replaces or redefines) the default version of paint by placing statements in the body of paint that draw Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply