summaryrefslogtreecommitdiffstats
path: root/sample/rmi/CountApplet.java
blob: 0bebdaf9291dddd0b1a783c35a6e3f659d385bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package sample.rmi;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javassist.rmi.ObjectImporter;
import javassist.rmi.ObjectNotFoundException;
import javassist.web.Viewer;

public class CountApplet extends Applet implements ActionListener {
    private Font font;
    private ObjectImporter importer;
    private Counter counter;
    private AlertDialog dialog;
    private String message;

    private String paramButton;
    private String paramName;

    public void init() {
	paramButton = getParameter("button");
	paramName = getParameter("name");
	importer = new ObjectImporter(this);
	commonInit();
    }

    /* call this method instead of init() if this program is not run
     * as an applet.
     */
    public void applicationInit() {
	paramButton = "OK";
	paramName = "counter";
	Viewer cl = (Viewer)getClass().getClassLoader();
	importer = new ObjectImporter(cl.getServer(), cl.getPort());
	commonInit();
    }

    private void commonInit() {
	font = new Font("SansSerif", Font.ITALIC, 40);
	Button b = new Button(paramButton);
	b.addActionListener(this);
	add(b);
	dialog = new AlertDialog();
	message = "???";
    }

    public void destroy() {
	dialog.dispose();
    }

    public void start() {
	try {
	    counter = (Counter)importer.lookupObject(paramName);
	    message = Integer.toString(counter.get());
	}
	catch (ObjectNotFoundException e) {
	    dialog.show(e.toString());
	}
    }

    public void actionPerformed(ActionEvent e) {
	counter.increase();
	message = Integer.toString(counter.get());
	repaint();
    }

    public void paint(Graphics g) {
	g.setFont(font);
	g.drawRect(50, 50, 100, 100);
	g.setColor(Color.blue);
	g.drawString(message, 60, 120);
    }

    public static void main(String[] args) {
	Frame f = new Frame("CountApplet");
	CountApplet ca = new CountApplet();
	f.add(ca);
	f.setSize(300, 300);
	ca.applicationInit();
	ca.start();
	f.setVisible(true);
    }
}