Programming with JApplets and JFrames
PURPOSE
REFERENCE
To convert from a Java Applet to an application:
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 }
To convert from a Java application into an 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>
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 }
OTHER EXAMPLES: