1 package sample.rmi; 2 3 import java.applet.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import javassist.tools.rmi.ObjectImporter; 7 import javassist.tools.rmi.ObjectNotFoundException; 8 import javassist.tools.web.Viewer; 9 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 } 56 catch (ObjectNotFoundException e) { 57 dialog.show(e.toString()); 58 } 59 } 60 61 public void actionPerformed(ActionEvent e) { 62 counter.increase(); 63 message = Integer.toString(counter.get()); 64 repaint(); 65 } 66 67 public void paint(Graphics g) { 68 g.setFont(font); 69 g.drawRect(50, 50, 100, 100); 70 g.setColor(Color.blue); 71 g.drawString(message, 60, 120); 72 } 73 74 public static void main(String[] args) { 75 Frame f = new Frame("CountApplet"); 76 CountApplet ca = new CountApplet(); 77 f.add(ca); 78 f.setSize(300, 300); 79 ca.applicationInit(); 80 ca.start(); 81 f.setVisible(true); 82 } 83 } 84