< Day Day Up > |
Applet BasicsWhen the Java language was introduced in 1995, the language feature that got the most attention was applets, Java programs that run on a World Wide Web page. Before Java, web pages were a combination of text, images, and forms that used Common Gateway Interface programs running on the computer that hosted the pages. These gateway programs required special access to the web server presenting the page. Applets require no special access and can be written by programmers of all skill levels. You'll write several during the span of these 24 hours. You can test them with any web browser that handles Java programs. Most of the Java programs you toured during the previous hour were all applets. Their structure differs from applications in several important ways, and they are designed specifically for presentation on the Web.
Unlike applications, applets do not have a main() block. Instead, they have several different sections that are handled depending on what is happening in the applet, as detailed fully during Hour 17. Two of the sections are the init() block statement and the paint() block. init() is short for initialization, and it is used to take care of anything that needs to be set up as an applet first runs. The paint() block is used to display anything that should be displayed. To see an applet version of the Root application, create a new file in your word processor and call it RootApplet.java. Enter the code in Listing 4.3 and save it when you're done. Listing 4.3. The Full Text of RootApplet.java1: import java.awt.*; 2: 3: public class RootApplet extends javax.swing.JApplet { 4: int number; 5: 6: public void init() { 7: number = 225; 8: } 9: 10: public void paint(Graphics screen) { 11: Graphics2D screen2D = (Graphics2D) screen; 12: screen2D.drawString("The square root of " + 13: number + 14: " is " + 15: Math.sqrt(number), 5, 50); 16: } 17: } Compile this file using your Java development software. If you are using the javac compiler tool in the JDK, type the following at a command line: javac RootApplet.java This program contains a lot of the same statements as the Root application that did the same thing. The primary difference is in how it is organized—the main() block has been replaced with an init() block and a paint() block.
Unlike applications, compiled Java applets cannot be tested using a Java interpreter. You have to put them on a web page and view that page in one of two ways:
To create a web page that can display the RootApplet program, return to your word processor and create a new file. Enter Listing 4.4 in that file and save it as RootApplet.html. Listing 4.4. The Full Text of RootApplet.html1: <applet code="RootApplet.class" height="100" width="300"> 2: </applet> This web page contains the bare minimum needed to display a Java applet on a web page. The applet tag is used to specify that a Java program is being put on the page, the code attribute provides the name of the applet, and the height and width attributes describe the size of the applet's display area. These items will be described in detail during Hour 17. To see this applet using the appletviewer tool included with Java Development Kit, type the following at a command line: appletviewer RootApplet.html To see it using your computer's default web browser, type the following command instead: RootApplet.html You also can load this page with your browser: Choose File, Open, then navigate to the folder that contains RootApplet.html and select the file. Figure 4.1 shows what the applet looks like when loaded with Mozilla Firefox. Figure 4.1. The RootApplet applet displayed with a web browser.
|
< Day Day Up > |