![]() |
Minimalist Programs NUGGET Jump Start |
|---|
|
There are two types of Java programs: applications and applets.
This nugget describes a hello world application and a hello
world applet.
|
|
Java provides a full featured environment that will sustain a wide-range of development activity. Programs that are NOT launched from a Web browser are called Java Applications. To write a Java application, open a new file in an editor and code the class structure in the example below. The name of the class in the source and the name of the file must be the same (including upper and lower case). The maximum length of the name is determined by the maximum length of a filename in the local file system. Here is the famous "Hello world" program as a Java application.
|
class HelloApplication
{
public static void main (String args[])
{
System.out.println ("Hello world!");
}
}
|
|
The name of the file for this program is HelloApplication.java.
If Nuggets for Java was installed into the There is a single function inside the HelloApplication class.
It is called To compile this program, switch to its directory and invoke the compiler followed by the file name of the source file.
javac HelloApplication.java
Every Java class compiles down to a .class file. The output of this compile is the executable HelloApplication.class file that contains instructions for the Java Virtual Machine. To run this program, invoke Java's Virtual Machine and pass it the
name of the class. It will print the message
java HelloApplication
Hello world!
The javac.exe and javac.exe programs are located in
Java's binary directory ( To construct a general purpose Java application a person simply needs
to construct a class with a |
|
If that is how to construct a regular Java program, how does one construct the special kind of Java program that is launched from a Web page? -- the steps are very similar. One opens a new file and codes a class structure in it. The name
of the file and the name of the class must be the same. But now, the
class statement must also say that it |
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello World!", 50, 25);
}
}
|
|
"Applet" is the name of the Java class that can ride in a Web page. When a class extends the Applet class, the new class becomes an extension of the Web riding class and so can be launched by a Web page. Since this kind of Java program works so closely with the Applet class, the Java program is called a Java applet. The second difference is that an applet has a The Graphics object is responsible for marking on the display
surface. In the example above, the Graphics object is given the name
g. Then the The parameters 50 and 25 identify the (x, y) position at which to place the string. This is precisely the lower left corner of the baseline upon which the string characters are drawn as measured in pixels from the upper left corner. Notice that there are two new classes referenced in this program -- the Applet class and the Graphics class. Whenever, a class outside of the current source file is referenced, an import statement must be coded to tell the compiler where to find the external class. Related classes are stored together in a unit called a package. The Applet class is part of the java.applet package and the Graphics class is part of the java.awt package. By coding the package name with the class name in the import statement, the compiler has all the information that is necessary to include the class in the compilation. The last way an applet differs from an application is that it must
be launched from a Web page rather than from a command line. The
special |
<HTML> <HEAD> <TITLE> HelloApplet </TITLE> </HEAD> <BODY> HelloApplet <HR> <APPLET CODE="HelloApplet" WIDTH=150 HEIGHT=50> </APPLET> <HR> </BODY> </HTML> |
|
The The phrase Although it is always possible to display an applet in a Web browser,
during development, it is desirable to test the applet with the
The following sequence compiles the HelloApplet and launches the
appletviewer to run it.
|
![]() Figure 1: HelloApplet |
|
|
C:\java\javanuggets\pages\examples\HelloApplet>javac HelloApplet.java C:\java\javanuggets\pages\examples\HelloApplet>appletviewer HelloApplet.html |
|
|
|
The Java programming language can be used to write programs that execute apart from a Web browser. These programs are called Java applications and can serve a wide variety of programming needs. The special kind of Java program that is attached to a Web page
is called a Java applet. It is able to be communicated across
the Internet in the same way that a Web page is communicated. When
a Web page and applet arrive at a Web browser, the applet is launched
by the browser and begins drawing on a section of the Web page.
|
|
by Noel Enete . . . www.enete.com . . . noel@enete.com |