When the Java language was introduced, the language feature that got the most attention was applets, Java programs that run on web pages. You can run them in any web browser that handles Java programs and test them with appletviewer, a tool included in the JDK that’s supported in NetBeans.
The structure of applets differs from applications. Unlike applications, applets do not have a main()
block. Instead, they have several sections that are handled depending on what is happening in the applet. Two 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 empty Java file with the class name RootApplet
. Enter the code in Listing 4.3 and make sure to save it when you’re done.
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: }
This program contains many of the same statements as the Root
application. The primary difference is in how it is organized. The main()
block has been replaced with an init()
block and a paint()
block.
The sample programs in this hour are provided primarily to introduce you to the way Java programs are structured. The main purpose of this hour is to get the programs to compile and see how they function when you run them. Some aspects of the programs will be introduced fully in the hours to come.
When you run the program in NetBeans (choose Run, Run File), the applet loads in the appletviewer tool, as shown in Figure 4.2.
Applets are slightly more complicated than applications because they must be able to run on a web page and coexist with other page elements in a browser. You learn how to create them in Hour 17, “Creating Interactive Web Programs.”
The appletviewer tool is useful for testing, but it gives the wrong impression about applets. They don’t run in their own windows as Java applications. Instead, they’re placed on web pages as if they are text, photos, or graphics. The applet is presented seamlessly with the rest of the page.
Figure 4.3 shows RootApplet
on a web page. The applet window is the white box that displays the program’s output: the square root of 225. The heading, paragraphs of text and lightbulb photo are ordinary elements of a web page.
Java applets can be static like the output of this project, but that’s a complete waste of the language. Applets usually display dynamic content as in a stock ticker, chat room client, or video games.
During this hour, you had a chance to create both a Java application and an applet. These two types of programs have several important differences in the way they function and the way they are created.
The next several hours continue to focus on applications as you become more experienced as a Java programmer. Applications are quicker to test because they don’t require you to create a web page to view them; they can be easier to create and more powerful as well.
Q. Do all arguments sent to a Java application have to be strings?
A. Java stores all arguments as strings when an application runs. When you want to use one of these arguments as an integer or some other nonstring type, you have to convert the value. You learn how to do this during Hour 11, “Describing What Your Object Is Like.”
Q. If applets run on web pages and applications run everywhere else, what are Java programs launched by Java Web Start?
A. Java Web Start is a way to launch Java applications from a web browser. A user clicks a link on a web page to run the program, which is easier than downloading it, running an installation wizard, and starting it like any other desktop software.
Although they’re run from a browser, Java Web Start programs are applications instead of applets. The application’s always up-to-date because it’s retrieved over the web from the program’s provider every time it is run.
Google Web Toolkit (GWT), a set of opensource tools for web programming, can convert a Java program into JavaScript, making it run faster and more reliably in web browsers without requiring a Java virtual machine.
Q. Does the line of succession to the British throne run out at some point?
A. Under Parliamentary law that has been in place since 1701, the British monarch must be a Protestant descendant of Sophia of Hanover, a German princess who was the heiress to the crown when the law was passed.
There are a finite number of people who are descendants of Sophia, so there’s always somebody last in the regal line. The British government only lists the first 38, so genealogists have attempted to fill out the rest of the list themselves.
The last person in the line of succession is Karin Vogel, a German pain therapist in her thirties. She was 4,973rd in line as of 2001, genealogists determined after an exhaustive search that took years. So if all the people ahead of her drop out of the running (to, say, spend more time learning Java programming), Vogel takes over the mortgage of Buckingham Palace and becomes Her Majesty Karin the First.
Vogel is Sophia’s great-great-great-great-great-great-great-great-granddaughter. She told the Wall Street Journal that becoming monarch would be “too stressful.”
If by the time you read this Prince William and Princess Kate have produced a Protestant child, Vogel drops to 4,974.
Test your knowledge of the material covered in this hour by answering the following questions.
1. Which type of Java program can be run inside a browser?
A. Applets
B. Applications
C. None
2. What does JVM stand for?
A. Journal of Vacation Marketing
B. Jacksonville Veterans Memorial
C. Java Virtual Machine
3. If you get into a fight with someone over the way to send information to a Java application, what are you doing?
C. Struggling over strings
B. Arguing about arguments
C. Feudin’ for functionality
1. A. Applets run as part of a web page, whereas applications are run everywhere else.
2. A, B, or C. Trick question! The initials stand for all three things, though Java Virtual Machine is the one you need to remember for the next 20 hours.
3. B. Applications receive information in the form of arguments. Can’t we all just get along?
If you’d like to apply your acumen of applets and applications, the following activities are suggested:
• Using the Root application as a guide, create a NewRoot application that can display the square root of 625.
• Using the Root application as a guide, create a NewRoot application that can display the square root of a number submitted as an argument.
To see a Java program that implements each of these activities, visit the book’s website at www.java24hours.com.
3.22.240.164