You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CountApplet.java 2.0KB

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