|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Extending AppletThe most powerful statement in HelloApplet is in the class definition HelloApplet extends Applet. With that one statement, weve said that our little class, HelloApplet, inherits all the methods and variables of Suns much larger class, Applet.Applet already knows how to communicate with the Web browser. It knows how to communicate with the graphical interface inside the browser window. It even knows how to redraw its content (although that content is empty). What it doesnt know is how to do any work. By extending Applet, we get a complete appletready to go. All we have to do is add content. Whats a public Class?Java provides several levels of accesspublic, protected, private, and a default level. Anyone who wants can access classes and members that are public. When you write an applet, you need to declare the applets class as public so that the Java environment can find it and run its methods. Using Applets paint() MethodAn applet draws into a graphical space provided by the Web browser. Whenever that space is covered (by another window) or disappears (because the user has minimized the window) it becomes invalid. When it becomes visible again, the applet must repaint itself. In fact, internally, Applet calls a method called repaint(). That repaint() method, in turn, calls paint(). In order to draw something into the applets graphical space, we need to do the work in paint(). The graphical space allocated for us by the browser is represented by a Graphics object, so we begin the paint() method by accepting that Graphics object (which we call theGraphics). The Graphics class supports over three dozen public methods, including drawLine(), draw3DRect(), and fillArc(). Because we want to put a String into the graphic, the method were interested in is drawString().drawString() takes three parametersthe String we want to draw, and the x and y coordinates where we want to start drawing. Weve chosen the coordinates 0 and 50 to start the String against the left margin, down a bit from the top.
Life Cycle of an AppletWeve seen Applets paint() method. As you might guess, repainting the screen is only one part of an applets life cycle. This section shows the various stages of an applets life and suggests various tasks you may want to undertake at each step. If youd like to see these steps in action, compile LifeCycle.java, given in Listing 37.4. You can construct an HTML file based on the pattern given in HelloApplet.htmljust change the name of the class file. Listing 37.4 LifeCycle.javaThis Applet Demonstrates the Stages in the Life of an Applet import java.applet.Applet; import java.awt.Graphics; public class LifeCycle extends Applet{ public LifeCycle() { System.out.println(Constructor running...); } public void init() { System.out.println(This is init.); } public void start() { System.out.println(Applet started.); } public void paint(Graphics theGraphics) { theGraphics.drawString(Hello, World!, 0, 50); System.out.println(Applet just painted.); } public void stop() { System.out.println(Applet stopped.); } public void destroy() { System.out.println(Applet destroyed.); } } If you run the LifeCycle applet from the appletviewer, youll receive standard out messages in the command prompt window. If youre using a Web browser, you can open the Java console. To open the Java console in Netscape Communicator 4.0, for example, choose Communicator, Java Console. Figure 37.8 shows the console in action.
ConstructorEvery class has a constructoryoull spot it because it has the same name as the class. You can put initialization code in the constructor. Restrict yourself to code that should be run only once during the life of the applet.
init() When the browser sees an <APPLET> tag, it instructs the Java class loader to load the specified class. The Java environment in the browser makes an instance of the class (by calling its constructor). It then calls the instances init() method. The init() method is the best place to put code that should run only once during your applets lifetime. Experiment with the LifeCycle applet in different browsers to see the circumstances under which the constructor and init() are called. start() and stop() After your applet is loaded and initialized, the Java environment calls start(). If the user leaves the page or minimizes it, the applets stop() method is called. The start() method will be called again when the user returns to the page. If your applet should take special action when the user enters or leaves the page, place the code for those actions in start() or stop().
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |