26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

120 Introduction <strong>to</strong> <strong>Java</strong> Applets Chapter 3<br />

Software Engineering Observation 3.2<br />

Generally, each applet should be less than 800 pixels wide and 600 pixels tall (most computer<br />

screens support these dimensions as the minimum width and height). 3.2<br />

Common <strong>Program</strong>ming Error 3.3<br />

Placing additional characters such as commas (,) between the attributes in the <br />

tag may cause the appletviewer or browser <strong>to</strong> produce an error message indicating a<br />

MissingResourceException when loading the applet. 3.3<br />

Common <strong>Program</strong>ming Error 3.4<br />

Forgetting the ending tag prevents the applet from loading in<strong>to</strong> the appletviewer<br />

or browser properly. 3.4<br />

Testing and Debugging Tip 3.5<br />

If you receive a MissingResourceException error message when loading an applet<br />

in<strong>to</strong> the appletviewer or a browser, check the tag in the HTML file carefully<br />

for syntax errors. Compare your HTML file <strong>to</strong> the file in Fig. 3.7 <strong>to</strong> confirm proper syntax. 3.5<br />

The appletviewer understands only the and HTML<br />

tags, so it is sometimes referred <strong>to</strong> as the “minimal browser” (it ignores all other HTML<br />

tags). The appletviewer is an ideal place <strong>to</strong> test an applet and ensure that it executes<br />

properly. Once the applet’s execution is verified, you can add the applet’s HTML tags <strong>to</strong><br />

an HTML file that will be viewed by people browsing the Internet.<br />

To execute the WelcomeApplet in the appletviewer open a command window,<br />

change <strong>to</strong> the direc<strong>to</strong>ry containing your applet and HTML file and type the command<br />

appletviewer WelcomeApplet.html<br />

Note that the appletviewer requires an HTML file <strong>to</strong> load an applet. This is different<br />

from the java interpreter for applications which requires only the class name of the application<br />

class. Also, the preceding command must be issued from the direc<strong>to</strong>ry in which the<br />

HTML file and the applet’s .class file are located.<br />

Common <strong>Program</strong>ming Error 3.5<br />

Running the appletviewer with a file name that does not end with .html or .htm is an<br />

error that prevents the appletviewer from loading your applet for execution. 3.5<br />

Portability Tip 3.2<br />

Test your applets in every browser used by people who view your applet. This will help ensure<br />

that people who view your applet experience the functionality you expect. [Note: A goal<br />

of the <strong>Java</strong> Plug-In (discussed later in the book) is <strong>to</strong> provide consistent applet execution<br />

across many different browsers.] 3.2<br />

3.4 Two More Simple Applets: Drawing Strings and Lines<br />

Let us consider another applet. An applet can draw Welcome <strong>to</strong> <strong>Java</strong> <strong>Program</strong>ming!<br />

several ways. For example, an applet can use two drawString statements in method<br />

paint <strong>to</strong> print multiple lines of text as in Fig. 3.8. The HTML file <strong>to</strong> load the applet in<strong>to</strong><br />

an applet container is shown in Fig. 3.9.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!