Converting Applications to Applets
Home ] Up ] Introduction to Applets ] A Simple Applet ] HTML Tags for Applets ] The Applet Life Cycle ] Multimedia and URLs ] Applet Context ] [ Converting Applications to Applets ]

 

 

A Java program can be both an applet and an application:

// ...
public class AppletApplication extends JApplet
{
    public void init() 
    { ... }
    
    // ...
    
    public static void main(String[] args) 
    { ... }
}

To make it work, the main method must create a frame for the applet to execute in.

It's convenient to create a frame to use for this purpose:

// ...
public class AppletFrame extends JFrame
{
    public AppletFrame(JApplet applet, int width, int height)
    {
        setTitle(applet.getClass.getName());
        setSize(width, height);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panel = new JPanel( new BorderLayout() );
        getContentPane().add(panel);

        panel.add(applet, BorderLayout.CENTER); // Add applet to frame

        applet.init();		// Init the applet
        setVisible(true);
        applet.start();		// Start the applet
    }

    // ...
}

So the main method of the AppletApplication should do the following:

// ...
public class AppletApplication extends JApplet
{
    public void init() 
    { ... }
    
    // ...
    
    public static void main(String[] args) 
    {
        new AppletFrame(new AppletApplication(), 620, 400);
    }
}

Unfortunately, this is not sufficient. If the applet tries to execute code like:

getAppletContext()

because there is no applet context in the AppletFrame.

The fix is to implement two interfaces in AppletFrame:
  • AppletStub
  • AppletContext
// ...
public class AppletFrame extends JFrame
	implements AppletStub, AppletContext
{
    // AppletStub methods
    public boolean isActive() { return true; }
    public URL getDocumentBase() { return null; }
    public URL getCodeBase() { return null; }
    public String getParameter(String name) { return ""; }
    public AppletContext getAppletContext() { return this; }
    public void appletResize(int width, int height) {}

    // AppletContext methods
    public AudioClip getAudioClip(URL url) { return null; }
    public Image getImage(URL url) { return null; }
    public Applet getApplet(String name) { return null; }
    public Enumeration getApplets() { return null; }
    public void showDocument(URL url) {}
    public void showDocument(URL url, String target) {}
    public void showStatus(String status) {}

    public AppletFrame(Applet applet, int width, int height)
    {
        setTitle(applet.getClass.getName());
        setSize(width, height);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel panel = new JPanel( new BorderLayout() );
        getContentPane().add(panel);

        panel.add(applet, BorderLayout.CENTER); // Add applet to frame

        applet.setStub(this);	// Set up the AppletStub
        applet.init();		// Init the applet
        setVisible(true);
        applet.start();		// Start the applet
    }

    // ...
}

Another possibility is to use inheritance to allow any applet to be executable as an application:

public class CalculatorAppletApplication
		extends AppletApplication
{
    public static void main(String[] args)
    {
        new AppletFrame(new CalculatorApplet(), 150, 100);
    }
}
 

This page was last modified on 02 October, 2007