Lecture 8A
  Standalone Java
  Simple Application
  Window App
  Applet to App 1
  Applet to App 2
  Applet Viewer
  Inner Classes
  
AnnonymousClass
  AdapterClass
 
Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture 8A : Converting Applet to Application
Converting an Applet into an Application

An applet can be converted with minimal changes into an application. (Short of recreating a full scale browser type environment with the AppletStub interface.)

The recipe goes as follows:

  • Create a Frame subclass to hold the applet.
  • In main() create an instance of the Frame.
  • Add the applet to the Frame.
  • Let main() call the init(), stop(), and start().
  • getParameter() will not work. Other techniques for passing the values must be used such as using an init() method with arguments that pass the parameters of interest.
  • getImage() also does not work. Use
     
    Toolkit.getDefaultToolkit.getImage(String img)

The example of double buffering that we discussed in Lecture 5B will be converted to an application.

We first create a "holder" application that will create a frame with a menubar for containing the applet.

We will modify the applet to add an initImg() to pass the image filename. We also modify the applet's init() to remove any non-compatible methods such as getImage().

/*
 * An application to run the double buffer
 * applet example.
 */

import java.awt.*;
import java.awt.event.*;

public class DBAppletHolder extends Frame
  implements ActionListener{

  public static void main(String [] args){

    // Pass the image name as a command line argument
    if( args.length != 1) {
      System.out.println(
          "Usage: java DBAppletHolder imageFilename");
      System.exit(0);
    }

    // Create an instance of this frame class
    // for holding the applet

    DBAppletHolder h= new DBAppletHolder();

    // Create an instance of the applet
    DoubleBufferedClipped DBApplet = new
       DoubleBufferedClipped();
    // Call init() to do whatever initialization can be
    // done outside of a browser environment.

    DBApplet.init();
    // Pass the applet the image name
    DBApplet.initImg(args[0]);
    h.add("Center", DBApplet);
 
    // Use an adapter to close the window
    WindowListener listener = new WindowAdapter() {
        public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    };
    h.addWindowListener(listener);

    h.pack();
    h.show();
  }
  // Add a menubar with only the Quit item.
  DBAppletHolder() {
    setTitle("Hello World");
    Menu m = new Menu("File");
    MenuItem mi = new MenuItem( "Quit" );
    mi.addActionListener( this );
    m.add(mi);
    MenuBar mb = new MenuBar();
    mb.add(m);
    setMenuBar(mb);
  }
  // Catch the Quit menuItem event to close the window.
  public void actionPerformed( ActionEvent e ) {
  if ( e.getActionCommand().equals("Quit") )
    dispose();
    System.exit(0);
  }
}

 

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey