Go to Bottom of Page

[Previous Section] [Next Section]

Chee Yap


Java applets are java programs that can be embedded in webpages and run. So there are three steps in creating applet applications:
(1) You first write and compile a java program.
(2) You create an html file that calls this java program.
(3) You place this html file in a web-accessible location.


[1] Java 2D Graphics, J.Knudsen, O'Reilly
[2] Core Web Programming, M.Hall and L.Brown, Sun.

[Previous Section] [Next Section]

1   Hello World Java Applet

A Simple Java Applet Program  

The following Applet reads an image file called ``gifIcon.gif'' and print a label with the text "Hello Icon!".

	 * @file	HelloIcon.java
	 * @author	Chee Yap (yap@cs.nyu.edu)
	 * @date	Jan 24, 2003
	 * @purpose	A first example of Java Applet
	 * @notes	Visualization Class, Spring 2003
	 * $Id: l.tex,v 1.3 2003/03/09 14:43:05 yap Exp yap $

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

	public class HelloIcon
		extends Applet {

	  // Declare variables
	  public int COUNT = 0;
	  private Image myImage;

	  // Init method
	    public void init() {
		setFont(new Font("Sanserif", Font.BOLD, 18));
		myImage = getImage(getDocumentBase(),
		add(new Label("Hello Icon!"));

	  // Paint method
	    public void paint(Graphics g) {
		g.drawImage(myImage, 0, 50, this);
	}//class HelloIcon

To compile the above problem, you must first write the program a file named HelloIcon.java. Note that the file named must agree with the by the name of the class in the above program. Use your favorite editor to do this. Then compile it using the command:

	> javac HelloIcon.java

The result is the compiled program called HelloIcon.class. This is the file that will be called by the web browser. Note that our example requires has an image file ``gifIcon.gif''. You can download all program files from this directory: http://cs.nyu.edu/~yap/classes/visual/03s/lect/progs/applet/

[Previous Section] [Next Section]

2   HTML Basics

Browsers understand the HTTP protocol. The contents read by a browser is found in a text file called an HTML file. We now create such an HTML file which tells the browser to load the HelloIcon.class that we just created.

In the html file below, we do two things:
(1) We announce the applet as "My First Applet".
(2) We load the applet program HelloIcon.class.
Note that single line comments are enclosed inside the matching pair <! ... >. Multiline comments are enclosed inside <!-- ... -->.

	<html>  <! this tells the browser the protocol>
		Tbe main part of the html document is called the "body". 
		In this simple example, there is nothing before the body,
		but in general, there will be a title for this
		html page, style files, etc.

		<h1> My First Applet</h1>	<! header >
		<APPLET CODE="HelloIcon.class"
			WIDTH=500 HEIGHT=300>
		  Error! You need a Java-enabled Browser!

Again, use your favorite editor to write this in a file named HelloIcon.html. In this case, the actual name is not important, though the file entension should be .html or .htm.

Finally, you must place this file in a place which is web-accessible. Assuming you have an account on a unix-based server named cs.nyu.edu that is web-accessible. Suppose your account (or user) name is yap. Then you should create a directory named public\_html which should be readable and executable by the world. Place your file HelloApplet.html in this directory, making it world-readable. In the same directory you should place HelloIcon.class and gifIcon.gif, both also world-readable. Now you are ready. Now any browser can access your Hello applet through the URL

On other systems, please ask your web administrator.

[Previous Section] [Next Section]

3   Parameters

In standard applications, we pass command line arguments. But applets receive parameters from their html files read by browsers. This requires two steps:

(1) In the Html file, you need to use the PARAM tags: These tags specify NAME-VALUE pairs to be passed to the applet. Consider a Hello applet that takes three arguments: "greeting", "who" and "fontsize":

(2) In your Java code, you use the getParameter() to access the NAME-VALUE pairs:

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

public class Hello extends Applet

String greeting, who; int fontsize;

public void init() setBackground(Color.black); setForeground(Color.white);

greeting = getParameter("greeting"); who = getParameter("who"); fontsize = Integer.parseInt(getParameter("fontsize"));

setFont(new Font("SansSerif", Font.BOLD, fontsize);

public void paint(Graphics g) Dimension dim = getSize(); // size of Applet if (greeting != null) g.drawString(greeting + " " + who + "!", 0, dim.height/2 ); // position to draw

[Previous Section] [Next Section] Go to Top of Page

File translated from TEX by TTH, version 3.01.
On 9 Mar 2003, 09:46.