Converting Java Applet to Application

Applets are programs which run on web browser and Application means the stand alone program in a computer. In Java we can convert Applets to Application and Application to Applet. Using this idea we can make any program run as either applet or as an application. Following are the steps for converting applets to Application. Step 1: Change init() method's name to the constructor. Java Applets contains init() method which is used for initializing the applet, this method should be changed to constructor for that class. This involves removing void(return type) of init(), because constructor don't return anything. e.g. public void init() should be changed to Application() Step 2: Replace "extends Applet" with "extends Frame" for the class. e.g. public class Application extends Applet should be changed to public class Application extends Frame Step 3: Create new main() method. Create object of the class in that method and set size of the frame and different properties as follows.
  1. public static void main(String[] args)
  2. {
  3. Application app=new Application();
  4. app.setSize(400,400);
  5. app.setVisible(true);
  6. app.setLayout(new FlowLayout());
  7. }
Step 4: Delete "import" statement for class Applet. Step 5: Add WindowListener's method to handle window events. Add implements WindowListener to the class declaration as follows.
  1. public class Application extends Frame implements WindowListener{}
Add code to register WindowListener to the frame.
  1. this.addWindowListener(this);// code for handling window event is present in the same class.
Add implementation for all the methods of the WindowListener interface.
  1. public void windowClosing(WindowEvent e)
  2. {
  3. dispose();
  4. System.exit(0);// normal exit of program
  5. }
  6.  
  7. public void windowOpened(WindowEvent e){}// simply add definition if no code to be added.
  8. public void windowIconified(WindowEvent e){}
  9. public void windowClosed(WindowEvent e){}
  10. public void windowDeiconified(WindowEvent e){}
  11. public void windowActivated(WindowEvent e){}
  12. public void windowDeactivated(WindowEvent e){}
Step 6: Remove getAudioClip(), getImage(),getCodeBase(), getDocumentBase() methods as these are used with Applet only.

Comments

Submitted byHank (not verified)on Sat, 12/19/2015 - 05:04

Thanx for the applet to app conversion code.....Tried several from web; yours was the only one that worked!

Add new comment