Putting A Jav Applet On Web Pag


  • Putting an Applet on a Web Page
    Applets are placed on a Web page in the same way that anything unusual is put on a page: HTML commands are used to describe the applet, and the Web browser loads it along with the other parts of the page. If you have used HTML to create a Web page, you know that it's a way to combine formatted text, images, sound, and other elements together. HTML uses special commands called tags that are surrounded by < and > marks, including for the display of images, for the insertion of a paragraph mark, and
    to center the text that follows until a
    tag is reached.
    The performance of some of these HTML tags can be affected by attributes that determine how they function. For example, SRC is an attribute of the tag, and it provides the name of the image file that should be displayed. The following is an example of an tag:
    You can place applets on a Web page by using an tag and several attributes. The following is an example of the HTML required to put an applet on a page:
    Sorry, no dice ... this requires a Java-enabled browser.
    The CODE attribute identifies the name of the applet's class file. If more than one class file is being used with an applet, CODE should refer to the main class file that is a subclass of the Applet class.
    If there is no CODEBASE attribute, all files associated with the applet should be in the same directory as the Web page that loads the program. CODEBASE should contain a reference to the directory or subdirectory where the applet and any related files can be found. In the preceding example, CODEBASE indicates that the StripYahtzee applet can be found in the javadir subdirectory.
    The HEIGHT and WIDTH attributes designate the exact size of the applet window on the Web page and must be big enough to handle the things you are displaying in your applet.
    In between the opening tag and the closing tag, you can provide an alternate of some kind for Web users whose browser software cannot run Java programs. In the preceding example, a line of text is displayed indicating that Java is required to play the game.
    Another attribute that you can use with applets is ALIGN. It designates how the applet will be displayed in relation to the surrounding material on the page, including text and graphics. Values include ALIGN="Left", ALIGN="Right", and others.
    A Sample Applet
    The first program that you wrote was a Java application that revealed a depressing fact about the U.S. financial condition--one minute's worth of the national debt. If it isn't too painful a prospect, you'll take a look at how applets are structured by writing the same program as an applet.
    Load your word processor and create a new file called BigDebtApplet.java. Enter the text of Listing 13.1 into the file and save it when you're done.
    Listing 13.1. The full text of BigDebtApplet.java.

     1: import java.awt.*;
     2:
     3: public class BigDebtApplet extends java.applet.Applet {
     4:     int debt;
     5:
     6:     public void init() {
     7:         debt = 59000000;
     8:         debt = debt / 1440;
     9:     }
    10:
    11:     public void paint(Graphics screen) {
    12:         screen.drawString("A minute's worth of debt is $" + debt, 5, 50);
    13:     }
    14: }

    This applet does not need to use the start(), stop(), or destroy() methods, so they are not included in the program. Compile the program with the javac compiler tool.
    Using the drawString() Method
    The drawString() method is one of the things you can use in a paint() method to display information. It is similar in function to System.out.println() statement, which cannot be used in an applet. The drawString() method is part of the Graphics class, so you must use it in the paint() method or another method that has the Graphics object that was sent to the paint() method.
    The following three arguments are sent to drawString():
    ·         The text to display, which can be several different strings and variables strung together with the + operator
    ·         The x position (in an (x,y) coordinate system) where the string should be displayed
    ·         The y position where the string should be displayed
    The (x,y) coordinate system in an applet is used with several methods. It begins with the (0,0) point in the upper-left corner of the applet window. Figure 13.1 shows how the (x,y) coordinate system works in conjunction with the statement on Line 12 of BigDebtApplet.java.
    Figure 13.1. <../art/13/13tja01.jpg> Drawing a string to an (x,y) position.
    Testing the BigDebtApplet Program
    Although you have compiled the BigDebtApplet program into a class file, you cannot run it using the java interpreter. If you do, you'll get an error message such as the following:
    In class BigDebtApplet: void main(String argv[]) is not defined
    The error occurs because the java interpreter runs Java applications beginning with the first statement of the main() block. To run an applet, you need to create a Web page that loads the applet. To create a Web page, open up a new file on your word processor and call it BigDebtApplet.html. Enter Listing 13.2 and then save the file.
    Listing 13.2. The full text of BigDebtApplet.html.

     1:
     2:
     3: The Big Debt Applet
     4:
     5:
     6:
     7: This a Java applet:
     8:
     9: You need a Java-enabled browser to see this.
    10:
    11:
    12:

    Normally, you can test the Java applets that you write using the appletviewer tool that comes with the Java Developer's Kit. You can see the output of the BigDebtApplet applet by typing the following:
    appletviewer BigDebtApplet.html
    However, appletviewer only runs the applets that are included in a Web page and does not handle any of the other elements such as text and images. To see the BigDebtApplet.html file, load it into a browser that can handle Java programs, such as the current versions of Microsoft Internet Explorer and Netscape Navigator. Figure 13.2 shows a screen capture of BigDebtApplet.html loaded into Internet Explorer.
    Figure 13.2. <../art/13/13tja02.jpg> The BigDebtApplet program on a Web page displayed by Microsoft Internet Explorer.

    Caution: At the time of this writing, the current versions of Netscape Navigator and Microsoft Internet Explorer do not support any new feature introduced with version 1.1 of the Java language. This hour's applet works, but many others in later hours do not. Use the appletviewer tool to run applets unless you know your browser software fully supports Java 1.1.

    Workshop: Enhancing the BigDebtApplet Project
    As a short exercise to close out the hour, you'll enhance the BigDebtApplet program by making it accumulate the debt over time, displaying how much the national debt grows each second.
    Open up a new file with your word processor and call it Ouch.java. Enter Listing 13.3 and save the file when you're done.
    Listing 13.3. The full text of Ouch.java.

     1: import java.awt.*;
     2: import java.util.*;
     3:
     4: public class Ouch extends java.applet.Applet {
     5:     int debt = 683;
     6:     int totalTime = 1;
     7:
     8:     public void paint(Graphics screen) {
     9:         screen.drawString(totalTime + " second's worth of debt is $"
    10:             + (debt * totalTime), 5, 30);
    11:         for (int i = 0; i < 5000000; i++);
    12:         totalTime++;
    13:         repaint();
    14:     }
    15: }

    This file uses an empty for loop in Line 11 to approximate the passage of a second's time. Whether it actually pauses for a second depends on your processor speed and anything else that's currently running on your computer. The call to repaint() in Line 13 at the end of the paint() method causes the paint() method to be called again, starting over at the beginning of the method on Line 9.
    To try out the program, you need to compile it with the javac compiler tool and create a Web page that runs the applet. Create a new file on your word processor and enter Listing 13.4 into the file. Save it when you're done.
    Listing 13.4. The full text of Ouch.html.

    1:
    2:

    This Web page only contains the HTML tags that are required to add an applet to a page. Load this Web page into the appletviewer tool by typing the following at a command line:
    appletviewer Ouch.html
    You will see an applet that begins with the calculation of a second's worth of debt. At a regular interval, another second's debt will be added. The following is an example of the text that is displayed as the applet runs:
    13 second's worth of debt is $8879
    Summary
    This hour was the first of several that will focus on the development of applets. You got a chance to become acquainted with the init() and paint() methods, which you will be using frequently when you're developing applets.
    Writing applets is a good way for beginners to develop their skills as Java programmers for the following reasons:
    ·         Applets are usually smaller in scope, making their creation a less daunting task.
    ·         You can find thousands of sample applets on the World Wide Web, including many with the source file available to learn from.
    ·         You can make applets available to a global audience at low to no cost through the Web, exposing your work to more people who can offer comments and suggestions.
    There's a "code war" of sorts afoot among the hundreds of Java programmers who are putting their work on the Web, and many new applets announced on sites like demonstrate new things that can be done with the language.

Post a Comment

Previous Post Next Post