|
Create a Java Source File
Create a file named HelloWorld.java with the Java code shown here: import java.applet.Applet;
public class HelloWorld extends Applet {
Compile the Source File Compile the source file using the Java compiler. Platform-Specific Details: Compiling a Java Source File Using the JDK UNIX:
DOS shell (Windows 95/NT):
MacOS:
If the compilation succeeds, the compiler creates a file named
Create an HTML File that Includes the Applet Using a text editor, create a file named Hello.html in the same directory
<HTML>
Run the Applet To run the applet, you need to load the HTML file into an application
that can run Java applets. This application might be a Java-compatible
browser or another Java applet viewing program, such as the Applet Viewer
provided in the JDK. To load the HTML file, you usually need to tell the
application the URL of the HTML file you've created. For example, you might
enter
http://www.cs.buffalo.edu/~perini/class/javamain.html Once you've successfully completed these steps, you should see something
The Anatomy of a Java Applet Now that you've seen a Java applet, you're probably wondering how it works. Remember that a Java applet is a program that adheres to a set of conventions that allows it to run within a Java-compatible browser. Here again is the code for the "Hello World" applet. import java.applet.Applet;
public class HelloWorld extends Applet {
Importing Classes and Packages The code above starts off with two import statements. By importing
Besides importing individual classes, you can also import entire packages. Here's an example: import java.applet.*;
public class HelloWorld extends Applet {
In the Java language, every class is in a package. If the source code for a class doesn't have a package statement at the top, declaring the package the class is in, then the class is in the default package. Defining an Applet Subclass Every applet must define a subclass of the Applet class. In the "Hello
The first bold line of the following listing begins a block that defines
the
import java.applet.Applet;
public class HelloWorld extends Applet {
The extends keyword indicates that HelloWorld is a subclass of the class whose name follows: Applet. From the Applet class, applets inherit a great deal of functionality. Perhaps most important is the ability to respond to browser requests. For example, when a Java-capable browser loads a page containing an applet, the browser sends a request to the applet, telling the applet to initialize itself and start executing. An applet isn't restricted to defining just one class. Besides the necessary Applet subclass, an applet can define additional custom classes. When the applet attempts to use a class, the application that's executing the applet first looks on the local host for the class. If the class isn't available locally, it's loaded from the location that the Applet subclass originated from. Implementing Applet Methods The HelloWorld applet implements just one method, the paint method.
The bold lines of the following listing implement the paint method. import java.applet.Applet;
public class HelloWorld extends Applet {
Every applet must implement one or more of the init, start, and paint
Besides the init, start, and paint methods, applets can implement two
more
Returning to the above code snippet, the Graphics object passed into
the paint method represents the applet's onscreen drawing context. The
first argument to the Graphics drawString method is the string to draw
onscreen. The second and third arguments are the (x,y) position of the
lower left corner of the text onscreen. This applet draws the string "Hello
world!" starting at location (50,25).
Running an Applet Applets are meant to be included in HTML pages. Using the <APPLET>
The bold lines of the following listing comprise the <APPLET> tag that includes the "Hello World" applet in an HTML page. <HTML>
Here is the output of my program:
The above <APPLET> tag specifies that the browser should load the class whose compiled code is in the file named HelloWorld.class. The browser looks for this file in the same directory as the HTML document that contains the tag. When the browser finds the class file, it loads it over the network, if necessary, onto the computer the browser is running on. The browser then creates an instance of the class. If you include an applet twice in one page, the browser loads the class file once and creates two instances of the class. The WIDTH and HEIGHT attributes are like the same attributes in an <IMG>
tag:
The <APPLET> Tag: When you build <APPLET> tags, keep in mind that words such as APPLET
and
< APPLET
CODEBASE = codebaseURL
CODE = appletFile
ALT = alternateText
NAME = appletInstanceName
WIDTH = pixels
ALIGN = alignment
VSPACE = pixels
< PARAM NAME = appletParameter1 VALUE
= value >
alternateHTML If the HTML page containing this <APPLET> tag is viewed
by a browser that doesn't understand the <APPLET> tag, then the browser
will ignore the <APPLET> and <PARAM> tags, instead interpreting any
other HTML code between the <APPLET> and </APPLET> tags. Java-compatible
browsers ignore this extra HTML code. You use the ALT=
In the online version of this tutorial, we use alternate HTML to show a snapshot of the applet running, with text explaining what the applet does. Other possibilities for this area area link to a page that is more useful for the Java-ignorant browser, or text that taunts the user for not having a Java-compatible browser. |
Go on to Page 2
For CS 763(Web Seminar) at SUNY Buffalo |