Programming with JApplets and JFrames

PURPOSE

REFERENCE

  1. We will use the Balloon example (class given below).

    To convert from a Java Applet to an application:

    1. Delete import java.applet.*;.
    2. Alter the class header so that it extends Frame rather than Applet (line 4 in BalloonFrame.java, below).
    3. Change the name of the init method to the name of the class, making the former init method the constructor for the class. Delete the word void in the declaration since constructors have no return type (line 9).
    4. Add an invocation of a method to set the title and the layout of the frame by adding the lines setTitle(<String>) and setLayout(new FlowLayout()); inside the constructor (lines 10 and 12).
    5. Declare (line 6) and add a button to close the window (lines 30-32 and 52-53) or implement WindowListener (not shown, but see reference books cited above).
    6. Create a new method called main, with the header public static void main (String[] args). This method should create a Frame object and should use the methods setSize(<int>,<int>) and setVisible(true) to display the frame (lines 37 through 41). See example below where the class is called BalloonFrame:

       1 import java.awt.*;
       2 import java.awt.event.*;
       3
       4 public class BalloonFrame extends Frame 
       5                           implements ActionListener {
       6    private Button grow, shrink, left, right, close;
       7    private Balloon theBalloon;
       8
       9    public BalloonFrame() {
      10       setTitle("Balloon");
      11
      12       setLayout(new FlowLayout());
      13
      14       grow = new Button("Grow");
      15       add(grow);
      16       grow.addActionListener(this);
      17
      18       shrink = new Button("Shrink");
      19       add(shrink);
      20       shrink.addActionListener(this);
      21
      22       left = new Button("Left");
      23       add(left);
      24       left.addActionListener(this);
      25
      26       right = new Button("Right");
      27       add(right);
      28       right.addActionListener(this);
      29
      30       close = new Button("Exit");
      31       add(close);
      32       close.addActionListener(this);
      33      
      34       theBalloon = new Balloon();
      35    }
      36
      37    public static void main(String args[]) {
      38       BalloonFrame f = new BalloonFrame();
      39       f.setSize(500, 500);
      40       f.setVisible(true);
      41    }
      42
      43    public void actionPerformed(ActionEvent event) {
      44       if (event.getSource() == grow)
      45          theBalloon.grow();
      46       if (event.getSource()  == shrink)
      47          theBalloon.shrink();
      48       if (event.getSource()  == left)
      49          theBalloon.moveLeft();
      50       if (event.getSource() == right)
      51          theBalloon.moveRight();
      52       if (event.getSource() == close)
      53          System.exit(0);
      54       repaint();
      55    }
      56
      57    public void paint(Graphics g) {
      58       theBalloon.display(g);
      59    }
      60 }
      
  2. To convert from a Java application into an applet:

    1. Create an HTML file with an applet tag to invoke the applet.

      For example, here is the HTML file for the Balloon example:

        <html>
        <body>
        <applet code="BalloonApplet.class" width=400 height=200>
        </applet>
        </body>
        </html>
      
    2. Add the line import java.applet.*; at the top (line 1 in BalloonApplet.java, below).
    3. Alter the class header so that it extends Applet rather than Frame (line 5).
    4. Delete the main method.
    5. Change the name of the constructor method from the name of the class to init (line 10).
    6. Note that there is no need to create a button to close the window as in the Frames example, above. Any such button and its associated action in the actionPerformed method can be deleted.
    7. For example, here is the entire BalloonApplet.java file:

       1 import java.applet.*;
       2 import java.awt.*;
       3 import java.awt.event.*;
       4
       5 public class BalloonApplet extends Applet implements ActionListener {
       6
       7    private Button grow, shrink, left, right;
       8    private Balloon theBalloon;
       9
      10    public void init() {
      11       grow = new Button("Grow");
      12       add(grow);
      13       grow.addActionListener(this);
      14
      15       shrink = new Button("Shrink");
      16       add(shrink);
      17       shrink.addActionListener(this);
      18
      19       left = new Button("Left");
      20       add(left);
      21       left.addActionListener(this);
      22
      23       right = new Button("Right");
      24       add(right);
      25       right.addActionListener(this);
      26
      27       theBalloon = new Balloon();
      28    }
      29
      30    public void actionPerformed(ActionEvent event) {
      31       if (event.getSource() == grow)
      32          theBalloon.grow();
      33       if (event.getSource() == shrink)
      34          theBalloon.shrink();
      35       if (event.getSource() == left)
      36          theBalloon.moveLeft();
      37       if (event.getSource() == right)
      38          theBalloon.moveRight();
      39       repaint();
      40    }
      41
      42    public void paint(Graphics g) {
      43       theBalloon.display(g);
      44    }
      45 }
      

    Balloon.java file used by both of the above programs:

     1 import java.awt.*;
     2
     3 public class Balloon {
     4
     5    private int diameter;
     6    private int x, y;
     7
     8    public Balloon() {
     9       diameter = 10;
    10       x = 20;
    11       y = 50;
    12    }
    13
    14    public void display(Graphics g) {
    15       g.drawOval(x, y, diameter, diameter);
    16    }
    17
    18    public void moveLeft() {
    19       x -= 10;
    20    }
    21
    22    public void moveRight() {
    23       x += 10;
    24    }
    25
    26    public void grow() {
    27       diameter += 5;
    28    }
    29
    30    public void shrink() {
    31       diameter -= 5;
    32    }
    33 }
    
REFERENCE FILES (click link to download):

OTHER EXAMPLES: