You are on page 1of 2

Turning an Application into an Applet

If you have an application that is communicating with the world outside with a Graphical User Interface (GUI), for example by using the classes and methods from AWT, then it is a matter of less than half an hour to turn it into an applet that can be integrated in a webpage. Step 1 Rewrite, if necessary your application, so that "main", consists only of a call to the constructor of a class that is creating a window. Step 2 We assume that the application is called "myClass.java", and that the call to its constructor does nt involve any parameter. Parameter are discussed further down. Add at the beginning the lines
import java.applet.*; import java.net.*;

If originally you had something like public


public class myClass extends Frame implements ActionListener

then you should replace this by


public class myClass extends Applet implements ActionListener

Change the name of the constructor of your class from "public myClass()" to "public void init()". Remove the calls to "pack()" and particularly "show()". Throw away "main" Once this is done, you must park a compiled version of "myClass.class" with worlreadability in a directory that can be accessed from outside, for example in your "~/public_html" directory.

html Page and Parameter


You must create an html page of the following format (we have replaced all opening brackets "<" by "{"):
{html> {head> {title> My New Applet {/title> {/head> {center> {applet code = myClass width = 700 height = 450> {param name = SIZE value = 8> Why can't you run applets? {/applet> {/center> {/body> {/html>

This calls the applet myClass.class (which is assumed to be available in the same directory. It creates a space for it of 700 x 450 pixels. A parameter by the name SIZE is passed with value 8. Inside your program, you can ask for the value of this parameter by writing
size = IO.stringToInt(getParameter("SIZE"));

Here IO.stringToInt is a static method skipping blanks and then coneverting a String into an int.

You might also like