Special Topics


This page presents information on several special topics related to Java programming.

  1. Using Java on the Web.




Using Java on the Web

One of the really strong points of Java is its use on the web. You can write small Java programs called Applets which can be imbedded in a web page.

Applet Code
First you must write the code for an Applet. The main program class must be a subclass of Applet as shown in the example below. Note that there is no main() function. Instead the applet inherits the start() function from Applet which is called by the browser when the web page is loaded.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

//============================================================
/** This class demonstrates the simplest of Java Applets */
//============================================================
public class HelloWorldApplet extends Applet
{
	//------------------------------------------------------
	/** Default constructor */
	//------------------------------------------------------
	public HelloWorldApplet()
	{
		this.setSize(300, 300);
	}
	
	//------------------------------------------------------
	/** Override the paint function so we can draw in the
	 *   applet's frame.
	 */
	//------------------------------------------------------
	public void paint(Graphics g)
	{
		g.setColor(Color.red);
		g.fillRect(0, 0, 300, 300);
		g.setColor(Color.black);
		g.drawString("Hello Java Applet World", 50, 100);
	}
}
This applet is very simple but, you can write very complex applets to add great features to a web page. However, be aware that there are some restrictions on what you can do with an applet. All of these are to maintain proper security on the computer where the web page and applet is being displayed and run. We usually say that Java has to play in a safe sandbox. Here are some of the things you can’t do in an Applet: After writing your applet code and compiling it to a .class file you must then create an HTML file to display the applet. A simple HTML file for displaying the above applet is shown here. If you are not familiar with web page development and HTML (HyperText Markup Language) then look for a good text on the subject at a local bookstore. It is not hard to learn but it is beyond the scope of this web page. If you are familiar with HTML then you will recognize that the applet is added to the page using a special APPLET tag.
<HTML>
<HEAD>
<TITLE>A Simple Java Applet</TITLE>
</HEAD>
<BODY>
Here is the output of a simple Java Applet:<BR>
<APPLET CODE="HelloWorldApplet.class" WIDTH=300 HEIGHT=300>
</APPLET>
</BODY>
</HTML>

Click here to see the applet displayed.


This site is still under construction