JAVA 
a summary of Java and it's use in WWW projects
Create a Java Source File  

Create a file named HelloWorld.java with the Java code shown here: 

     import java.applet.Applet; 
     import java.awt.Graphics; 

     public class HelloWorld extends Applet { 
           public void paint(Graphics g) { 
                 g.drawString("Hello world!", 50, 25); 
           } 
     } 

Compile the Source File  

Compile the source file using the Java compiler. 

Platform-Specific Details: Compiling a Java Source File Using the JDK 

UNIX: 
javac HelloWorld.java 
then Change your permissions to be world readable: 
chmod 755 * 

DOS shell (Windows 95/NT): 
javac HelloWorld.java 

MacOS: 
Drag the HelloWorld.java file icon onto the Java Compiler icon. 

If the compilation succeeds, the compiler creates a file named 
HelloWorld.class in the same directory (folder) as the Java source file 
(HelloWorld.java). This class file contains Java bytecodes. 

Create an HTML File that Includes the Applet 

Using a text editor, create a file named Hello.html in the same directory 
that contains HelloWorld.class. This HTML file should contain the 
following text: 

<HTML> 
<HEAD> 
<TITLE> A Simple Program </TITLE> 
</HEAD> 
<BODY> 
Here is the output of my program: 
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25> 
</APPLET> 
</BODY> 
</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 
something like the following into a browser's URL or Location field: 

http://www.cs.buffalo.edu/~perini/class/javamain.html 

Once you've successfully completed these steps, you should see something 
like this in the browser window: 



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; 
import java.awt.Graphics; 

public class HelloWorld extends Applet { 
public void paint(Graphics g) { 
g.drawString("Hello world!", 50, 25); 

Importing Classes and Packages 

The code above starts off with two import statements. By importing 
classes or packages, a class can more easily refer to classes in other 
packages. In the Java language, packages are used to group classes, 
similar to the way libraries are used to group C functions. Importing Classes and Packages gives you more information about packages and the import statement. 

Besides importing individual classes, you can also import entire packages. 

Here's an example: 

import java.applet.*; 
import java.awt.*; 

public class HelloWorld extends Applet { 
public void paint(Graphics g) { 
g.drawString("Hello world!", 50, 25); 

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 
World" applet, this subclass is called HelloWorld. Applets inherit a great 
deal of functionality from the Applet class, ranging from communication 
with the browser to the ability to present a graphical user interface (GUI). 

The first bold line of the following listing begins a block that defines the 
HelloWorld class. 

import java.applet.Applet; 
import java.awt.Graphics; 

public class HelloWorld extends Applet { 
public void paint(Graphics g) { 
g.drawString("Hello world!", 50, 25); 

} 

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. 
Every applet must implement at least one of the following methods: init, 
start, or paint. Unlike Java applications, applets do not need to implement a main method. 

The bold lines of the following listing implement the paint method. 

import java.applet.Applet; 
import java.awt.Graphics; 

public class HelloWorld extends Applet { 
public void paint(Graphics g) { 
g.drawString("Hello world!", 50, 25); 
} 

Every applet must implement one or more of the init, start, and paint 
methods. 

Besides the init, start, and paint methods, applets can implement two more 
methods that the browser calls when a major event occurs (such as leaving the applet's page): stop and destroy. Applets can implement any number of other methods, as well. 

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). 
The applet's coordinate system starts at (0,0), which is at the upper left corner of the applet's display area. 

Running an Applet 

Applets are meant to be included in HTML pages. Using the <APPLET> 
tag, you specify (at a minimum) the location of the Applet subclass and 
the dimensions of the applet's onscreen display area. When a Java-capable browser encounters an <APPLET> tag, it reserves onscreen space for the applet, loads the Applet subclass onto the computer the browser is executing on, and creates an instance of the Applet subclass. 

The bold lines of the following listing comprise the <APPLET> tag that includes the "Hello World" applet in an HTML page. 

<HTML> 
<HEAD> 
<TITLE> A Simple Program </TITLE> 
</HEAD> 
<BODY> 

Here is the output of my program: 
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25> 
</APPLET> 
</BODY> 
</HTML> 

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: 
They specify the size in pixels of the applet's display area. Most browsers do not let the applet resize itself to be larger or smaller than this display area. For example, every bit of drawing that the "Hello World" applet does in its paint method occurs within the 150x25-pixel display area that the above <APPLET> tag reserves for it. 

The <APPLET> Tag: 

When you build <APPLET> tags, keep in mind that words such as APPLET and 
CODEBASE can be typed in either as shown or in any mixture of uppercase and 
lowercase. Bold font indicates something you should type in exactly as shown 
(except that letters don't need to be uppercase). Italic font indicates that you must 
substitute a value for the word in italics. Square brackets ([ and ]) indicate that the 
contents of the brackets are optional. 

< APPLET  
[CODEBASE = codebaseURL] 
CODE = appletFile  
[ALT = alternateText
[NAME = appletInstanceName
WIDTH = pixels 
HEIGHT = pixels 
[ALIGN = alignment
[VSPACE = pixels
[HSPACE = pixels

[< PARAM NAME = appletParameter1 VALUE = value >] 
[< PARAM NAME = appletParameter2 VALUE = value >] 
. . . 
[alternateHTML
</APPLET

CODEBASE = codebaseURL 
This optional attribute specifies the base URL of the applet -- the directory or folder that contains the applet's code. If this attribute is not specified, then the document's URL is used. 

CODE = appletFile 
This required attribute gives the name of the file that contains the applet's 
compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute. 

ALT = alternateText 
This optional attribute specifies any text that should be displayed if the browser 
understands the APPLET tag but can't run Java applets. 

NAME = appletInstanceName 
This optional attribute specifies a name for the applet instance, which makes it possible for applets on the same page to find (and communicate with) each other. 

WIDTH = pixels 
HEIGHT = pixels 
These required attributes give the initial width and height (in pixels) of the 
applet display area, not counting any windows or dialogs that the applet brings 
up. 

ALIGN = alignment 
This required attribute specifies the alignment of the applet. The possible 
values of this attribute are the same (and have the same effects) as those for 
the IMG tag: left, right, top, texttop, middle, absmiddle, baseline, bottom, 
absbottom. 

VSPACE = pixels 
HSPACE = pixels 
These optional attributes specify the number of pixels above and below the applet (VSPACE) and on each side of the applet (HSPACE). They're treated the same way as the IMG tag's VSPACE and HSPACE attributes. 

< PARAM NAME = appletParameter1 VALUE = value
<PARAM> tags are the only way to specify applet-specific parameters. Applets read user-specified values for parameters with the getParameter() method. See Defining and Using Applet Parameters for information about the getParameter() method. 

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=
tag to give an alternate to the java applet.

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
This page was created by Bob Perini 
For CS 763(Web Seminar) at SUNY Buffalo