Web Design With Java: What Is A Java Applet And How Do You Create One?

Learn more about what Java is and how (and why) you'd create and use an applet computer code in your web page designs.

Java is a programming language, developed by Sun Microsystems, which is now widely used. The benefit of Java is that a developer can write something in Java and it can run on any operating system (Windows, Linux, and UNIX). Java accomplishes this by splitting the language up between the code and the JVM (Java Virtual Machine). The JVM is specific to the operating system (OS) and translates the code into something that runs on that OS.

Developers can use Java to create entire applications, server based programs or applets. Java applets are small Java programs which can be embedded in a web page. They are run using the JVM installed in the Web Browser. Not all web browsers have a JVM installed, but one can be freely installed from Sun at http://www.java.com.

Embedding applets into a Web Page is easy, they are formatted just like other HTML code and use the tag to define where the applet is located in the page and other properties. The Java code for the applet can be written directly into the Web Page, or more commonly, is located at another location and referenced in the tag.

Applets can do nearly anything that programmers can dream up. They can be very simple such as a little box that displays the current time to complex games or applications like spreadsheets or word processors. However, since the applet runs inside the context of the Web Browser, it is restricted to doing things that the browser allows.

It is possible to code an applet to do harmful or malicious things to the user's computer. It is critical to make sure when loading an applet that you know where it came from. Most web browsers by default will ask you to "trust" the applet to run.

So, how does one go about creating a Java applet?

Java is based on classes. These object classes are specific code that calls on certain functionality when it is invoked. The JVM comes with a great deal of pre-loaded classes that you can use for most of the things you need. Developers can then extend these classes with classes of their own design to do very specific functions.



The basic starting point for most programming is what is called the "Hello World" example. That is usually just a box that says Hello World. To create a Hello World applet, you can follow these steps:

Note: All applets have to reference a pre-defined Java object called Applet. You do this by creating your Java class as an extension of the Applet object:

public class helloWorld extends Applet

Next you add the code of what you want the applet to do:

public class helloWorld extends Applet {

public void paint(Graphics g) {

g.drawString("Hello world!", 50, 25);

}

}

Finally, you must add some import statements so that the JVM knows where to get the Java code you referenced:

import java.applet.Applet;

import java.awt.Graphics;

public class helloWorld extends Applet {

public void paint(Graphics g) {

g.drawString("Hello world!", 50, 25);

}

}

Once that is done, you need to save the file and compile it. Compiling is different depending on what development environment you are using, but if you use just the standard Sun JDK, it is done via a command line program called javac. Once you compile the Java code you will end up with a file called helloWord.class.

The next step is to write the Web page in HTML to display this applet. The page can be very simple, but must include the tag:

A Simple Program

Here is the output of my program:

Here we use two optional tags width and height to say how large a space (in pixels) to make for the applet on the web page.

Once you save the HTML file, all you need to do is put your helloWorld.class file in the same directory, and then load the HTML page in your browser and you should be able to see the results.

You can also download applets already created by other people and posted on the Internet to use in your Web pages. Many resources exist such as Java Boutique (http://javaboutique.internet.com/applets/) that has hundreds of free applets for you to use.

© High Speed Ventures 2011