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:
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:
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:
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.
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:
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.






MultiQuote





|