/Users/lyon/j4p/src/javassist/sample/rmi/CountApplet.java

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