Full Version: Application to JApplet and reverse
Dream.In.Code > Programming Tutorials > Java Tutorials
William_Wilson
JApplets and Standard Java Applications

This tutorial is meant to teach both the differences and similarities between an applet and your generic Java application. I will also outline the base of each type and how to convert from one to the other.

*It is assumed that the basic terminology and uses of Java are already understood, or will be learned elsewhere.

Your garden variety java application has a class declaration, methods, constructors, and most often a main method. These are the basics for any single file java application.
A simple example:
CODE

public class Test
{
    //empty constructor
    public Test()
    {
        System.out.println(“Java”);
    }
    //single variable constructor
    public Test(String s)
    {
        System.out.println(s);
    }
    //method
    public void method()
    {
        System.out.println(“method”);
    }

    public  static void main(String[] args)
    {
        Test t = new Test();
        t.method();
    }
}


There is little difference between this and an applet, when you break it down. There are a few specific methods, which must be present in an applet, but besides this there is little difference between the two.

An applet is basically a GUI that does not have a frame. The frame is supplied by the browser window is your frame instead. This is where it becomes apparent why most Java programmers insist on designing your Graphical User Interfaces as a panel, then applying the panel, or panels to a JFrame.

Required Methods:
public void init() {}
this method is run when the applet is being loaded into memory or constructed. Think of this as the applet constructor.

public void start() {}
this is run once all preparations are done, and the code is actually beginning to run. Place any splash screens or intro stuff here.

public void stop() {}
this is run when the applet has been terminated, while memory is being freed and the code is stopping.

public void destroy() {}
this method is called just before a system exit. Notifying that the applet has finished and cannot be run again without reloading it.

**NOTE: method descritpions are borrowed from my code snippet “Applet Base”.


Application to JApplet:
A simple example base application:
CODE

import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;  
import javax.swing.event.*;

public class Test extends JFrame
{
    //empty constructor
    public Test()
    {
        System.out.println("Java");
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               getContentPane().add(new panel());        // set panel on frame

                setSize(400,600);               // Set the size of the frame
                setVisible(true);               // Show the frame
    }
    //single variable constructor
    public Test(String s)
    {
        System.out.println(s);
    }
    //method
    public void method()
    {
        System.out.println("method");
    }
    //Panel
    public class panel extends JPanel
    {
        public panel()
        {
            //this is where the diplay items go
        }
    }
    public  static void main(String[] args)
    {
        new Test();
    }
}

Now to separate the JFrame from the class declaration. This requires adding a method to apply the JPanel to the JFrame we will create in the main method. This serves two purposes:
1) The code will more closely resemble a JApplet
2) Allows the code to be viewed in a JFrame even though it can be loaded as a JApplet as well.

Modification:
CODE

import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;  
import javax.swing.event.*;

public class Test
{
    //empty constructor
    public Test()
    {
        System.out.println("Java");
    }
    //single variable constructor
    public Test(String s)
    {
        System.out.println(s);
    }
    //method
    public void method()
    {
        System.out.println("method");
    }
    //Apply the Panel
    public JPanel setPanel()
    {
        return new panel();
    }
    //Panel
    public class panel extends JPanel
    {
        public panel()
        {
            //this is where the diplay items go
        }
    }
    public  static void main(String[] args)
    {
        JFrame frame = new JFrame("Frame");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Test t = new Test();
                JPanel d = t.setPanel();        // get panel
                frame.getContentPane().add(d);        // set panel on frame
    
                frame.setSize(400,600);           // Set the size of the frame
                frame.setVisible(true);           // Show the frame
    }
}

Now to add the methods required in a JApplet, along with a few specific changes:
1) Test class extends JApplet.
2) The init() method should initialize the panel, required in a JApplet (not main method).
3) Remove the setPanel() method.
4) Add a call to init() in the main method, which simulates the browser call.
CODE

import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;  
import javax.swing.event.*;

public class Test extends JApplet
{
    //empty constructor
    public Test()
    {
        System.out.println("Java");
    }
    //single variable constructor
    public Test(String s)
    {
        System.out.println(s);
    }
    //method
    public void method()
    {
        System.out.println("method");
    }
    //init
    public void init()
    {
                  System.out.println("Applet initializing");
                  getContentPane().add(new panel());
        method();
        }
        //start
    public void start()
        {
                System.out.println("Applet starting");
        }
        //stop
        public void stop()
        {
                System.out.println("Applet stopping");
        }
        //destroy
        public void destroy()
        {
                System.out.println("Applet destroyed");
        }
    //Panel
    public class panel extends JPanel
    {
        public panel()
        {
            //this is where the diplay items go
        }
    }
    public  static void main(String[] args)
    {
        JFrame frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Test   t = new Test();
        t.init();                // simulate browser call(1)    
        frame.setSize(400,600);           // Set the size of the frame
        frame.setVisible(true);           // Show the frame
    }
}

That’s all there is to it really. Using the init() method to initialize your GUI, then calls method() to start with the actual class action.
start() is called next in a browser, and would be a suitable place to put these calls as well. As far as this environment is concerned, init() is the better way to go.

JApplet to Application:
Reversing this process, creating an application from an applet, is just as simple. Exchange the extended JApplet with JFrame, and add a main method which creates a call for a new Class(); be sure the constructor calls init(), then sets the variables for display:
CODE

import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;  
import javax.swing.event.*;

public class Test extends JFrame
{
    //empty constructor
    public Test()
    {
        System.out.println("Java");
        init();                // simulate browser call(1)
        setSize(400,600);           // Set the size of the frame
        setVisible(true);           // Show the frame
    }
    //single variable constructor
    public Test(String s)
    {
        System.out.println(s);
    }
    //method
    public void method()
    {
        System.out.println("method");
    }
    //init
    public void init()
    {
              System.out.println("Applet initializing");
              getContentPane().add(new panel());
              method();
        }
        //start
       public void start()
        {
            System.out.println("Applet starting");
        }
        //stop
        public void stop()
        {
            System.out.println("Applet stopping");
        }
        //destroy
        public void destroy()
        {
            System.out.println("Applet destroyed");
        }
    //Panel
    public class panel extends JPanel
    {
        public panel()
        {
            //this is where the diplay items go
        }
    }
    public  static void main(String[] args)
    {
        new Test();
    }
}

If you understand the conversion well enough, this conversion makes it easier to understand the first conversion. The middle step from application to applet is not completely necessary, but I felt it would make the conversion more simple, though smaller steps.
cambertaylor
Thank you so much for posting this. This is what I've been reading about in Head First Java and when I tried to implement it my compiler coughed up the error that it could not recognize symbol and the carrot was under the g in get. But I did copy and paste your code into the compiler just to see if it would run and it came through perfiect. Using this example I think I can now program my homeworkof adding a GUI to the mortgage calculation using a combo box to display term of loan and interest rate. I may post for help later on (I'm running into work this morning) Because I'm still not too clear on setting up the combobox and getting the array of terms right in the claculations that are needed. This is my second Java class and I'm understaning most of it but there are still little bumps that I get caught on. Again thank you for posting this. It is a great help.
s_federici
William,
I found your tutorial really very interesting! I tried to apply it to an old applet I had (not a JApplet, an awt Applet). When I try to run the application, I get this error:

Exception in thread "main" java.lang.NullPointerException
at BreakOut$panel.<init>(BreakOut.java:45)
at BreakOut.init(BreakOut.java:125)
at BreakOut.<init>(BreakOut.java:29)
at BreakOut.main(BreakOut.java:57)

I'm a java newbie so I really don't know how I could fix it. Would you mind having a look at my code? Thanks!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.