Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Applet Basics
When 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 gateway programs running on the computer that hosted the pages. These gateway programs required special access to the Web server presenting the page, so most Web users did not have the ability to use them. Writing them required even more expertise.
In contrast, programmers of all skill levels can write Java applets, and you'll write several during the span of these 24 hours. You can test them with any Web browser that handles Java programs, and put one on a Web page without requiring any special access from a Web provider. 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 World Wide 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.
Example 4.3. The Full Text of RootApplet.java
1: 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 SDK, 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 main 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:
- Use a Web browser that can handle Java 2 applets by using Sun's Java Plug-in, which requires special configuration of the Web page.
- Use the appletviewer tool that comes with the Software Development Kit.
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.
Example 4.4. The Full Text of RootApplet.html
1: <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 Software 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 instead:
RootApplet.html
You can also load this page in 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 Internet Explorer.
Figure 4.1 The RootApplet applet displayed with a Web browser.
Sending Parameters to Applets | Next Section

Account Sign In
View your cart