01: package sample.rmi;
02:
03: import java.applet.*;
04: import java.awt.*;
05: import java.awt.event.*;
06: import javassist.tools.rmi.ObjectImporter;
07: import javassist.tools.rmi.ObjectNotFoundException;
08: import javassist.tools.web.Viewer;
09:
10: public class CountApplet extends Applet implements ActionListener {
11: private Font font;
12: private ObjectImporter importer;
13: private Counter counter;
14: private AlertDialog dialog;
15: private String message;
16:
17: private String paramButton;
18: private String paramName;
19:
20: public void init() {
21: paramButton = getParameter("button");
22: paramName = getParameter("name");
23: importer = new ObjectImporter(this );
24: commonInit();
25: }
26:
27: /* call this method instead of init() if this program is not run
28: * as an applet.
29: */
30: public void applicationInit() {
31: paramButton = "OK";
32: paramName = "counter";
33: Viewer cl = (Viewer) getClass().getClassLoader();
34: importer = new ObjectImporter(cl.getServer(), cl.getPort());
35: commonInit();
36: }
37:
38: private void commonInit() {
39: font = new Font("SansSerif", Font.ITALIC, 40);
40: Button b = new Button(paramButton);
41: b.addActionListener(this );
42: add(b);
43: dialog = new AlertDialog();
44: message = "???";
45: }
46:
47: public void destroy() {
48: dialog.dispose();
49: }
50:
51: public void start() {
52: try {
53: counter = (Counter) importer.lookupObject(paramName);
54: message = Integer.toString(counter.get());
55: } catch (ObjectNotFoundException e) {
56: dialog.show(e.toString());
57: }
58: }
59:
60: public void actionPerformed(ActionEvent e) {
61: counter.increase();
62: message = Integer.toString(counter.get());
63: repaint();
64: }
65:
66: public void paint(Graphics g) {
67: g.setFont(font);
68: g.drawRect(50, 50, 100, 100);
69: g.setColor(Color.blue);
70: g.drawString(message, 60, 120);
71: }
72:
73: public static void main(String[] args) {
74: Frame f = new Frame("CountApplet");
75: CountApplet ca = new CountApplet();
76: f.add(ca);
77: f.setSize(300, 300);
78: ca.applicationInit();
79: ca.start();
80: f.setVisible(true);
81: }
82: }
|