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
A Sample Applet
For your first applet, you will display the string "Saluton mondo!" in the applet window, the traditional Esperanto greeting that is becoming more traditional by the hour. You'll take a look at how applets are structured by re-creating the Saluton application from Hour 2, "Writing Your First Program," as a program that can run on a Web page.
Load your word processor and create a new file called SalutonApplet.java. Enter the text of Listing 17.1 into the file and save it when you're done.
Example 17.1. The Full Text of SalutonApplet.java
1: import java.awt.*;
2:
3: public class SalutonApplet extends javax.swing.JApplet {
4: String greeting;
5:
6: public void init() {
7: greeting = "Saluton mondo!";
8: }
9:
10: public void paint(Graphics screen) {
11: Graphics2D screen2D = (Graphics2D)screen;
12: screen2D.drawString(greeting, 25, 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, if you're an SDK user, or another tool.
Drawing in An Applet Window
Text is displayed in an applet window by using the drawString() method of the Graphics2D class, which draws text in a graphical user interface component.
The drawString() method is similar in function to the System.out.println() method that displays information to the system's standard output device.
Before you can use the drawString() method, you must have a Graphics2D object that represents the applet window.
The paint() method of all applets includes a Graphics object as its only argument. This object represents the applet window, so it can be used to create a Graphics2D object that also represents the window.
As you might suspect, you use casting to convert a Graphics object into a Graphics2D object. Graphics2D is a subclass of Graphics. The following statement casts a Graphics object named screen into a Graphics2D object named screen2D.
Graphics2D screen2D = (Graphics2D)screen;
You'll see a statement like this often in the paint() method of applets.
When you have created a Graphics2D object like this, you can call its drawString() method to display text on the area represented by the object.
The following three arguments are sent to drawString():
- The text to display, which can be several different strings and variables pasted 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 17.1 shows how the (x,y) coordinate system works in conjunction with the statement on Line 12 of SalutonApplet.java.
Figure 17.1 Drawing a string to an (x,y) position.
Testing the SalutonApplet Program
Although you have compiled the SalutonApplet program into a class file, you cannot run it using a Java interpreter such as java. If you do, you'll get an error message looking like this:
Exception in thread "main" java.lang.NoSuchMethodError: main
The error occurs because a Java interpreter runs applications by calling its main() method. Applets don't include this method. Instead, 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 SalutonApplet.html. Enter Listing 17.2 and then save the file.
Example 17.2. The Full Text of SalutonApplet.html
1: <html> 2: <head> 3: <title>Saluton Mondo!</title> 4: </head> 5: <body bgcolor="#000000" text="#FF00FF"> 6: <center> 7: This a Java applet:<br> 8: <applet code="SalutonApplet.class" height=150 width=300> 9: You need a Java-enabled browser to see this. 10: </applet> 11: </body> 12: </html>
All applets you write can be tested with the appletviewer tool that comes with the Software Development Kit. You can see the output of the SalutonApplet applet by typing the following:
appletviewer SalutonApplet.html
One thing to note about appletviewer is that it 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.
Applets can also be loaded by Web browsers, if they are equipped with the Java Plug-in. To attempt this at a command line, type the following command:
SalutonApplet.html
You can also choose File, Open from the browser's menu to find and open the page.
Figure 17.2 shows a screen capture of SalutonApplet loaded by Internet Explorer.
Figure 17.2 The SalutonApplet program running on a Web page.
If you can't get this applet to run in Internet Explorer or another Web browser, the most likely reason is that the browser needs the Java Plug-in.
The Java Plug-in | Next Section

Account Sign In
View your cart