home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / beans / simplebean / example / TestAcme09Bean.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.9 KB  |  113 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import acme.beans.Acme09Bean;
  18. import java.awt.*;
  19. import java.applet.*;
  20.  
  21. public class TestAcme09Bean extends Applet
  22. {
  23.    String msgStr;
  24.    private Acme09Bean b;
  25.  
  26.    public void init()
  27.    {
  28.      b = new Acme09Bean();
  29.      add(b);
  30.       setFont(new Font("TimesRoman", Font.BOLD, 24));
  31.       msgStr = "Press any key...";
  32.       setForeground(Color.blue);
  33.       add(new Button("make action event"));
  34.       setSize(400,200);
  35.    }
  36.  
  37.    public void start()
  38.    {
  39.       requestFocus();
  40.    }
  41.  
  42.    public boolean keyDown(Event e,  int key)
  43.    {
  44.       msgStr = "\"" + (char)key + "\"" + " key pressed";
  45.       if ((char)key == 'q') {
  46.         System.exit(0);
  47.         System.out.println("Got a 'q'");
  48.       }
  49.       switch ((char)key) {
  50.       case '+':
  51.       case 'i':
  52.         System.out.println("Got a '+'");
  53.         b.incrementCount();
  54.         b.updateLabel();
  55.         return true;
  56.       case '-':
  57.       case 'd':
  58.         System.out.println("Got a '-'");
  59.         b.decrementCount();
  60.         b.updateLabel();
  61.         return true;
  62.  
  63.       }
  64.       repaint();
  65.         System.out.println("key: " + key);
  66.       return true;
  67.    }
  68.  
  69.    public boolean keyUp(Event e,  int key)
  70.    {
  71.       msgStr = "\"" + (char)key + "\"" + " key released";
  72.       repaint();
  73.       return true;
  74.    }
  75.  
  76.    public boolean gotFocus(Event e,  Object arg)
  77.    {
  78.       msgStr = "Got Focus";
  79.       if (arg!= null)
  80.          msgStr = msgStr + " arg: " + arg.toString();
  81.       repaint();
  82.       return true;
  83.    }
  84.  
  85.    public boolean lostFocus(Event e,  Object arg)
  86.    {
  87.       msgStr = "Lost Focus" ;
  88.       if (arg != null)
  89.          msgStr = msgStr + " arg: " + arg.toString();
  90.       repaint();
  91.       return true;
  92.    }
  93.  
  94.    public boolean action(Event e,  Object arg)
  95.    {
  96.       msgStr = "Action Event." ;
  97.       if (arg != null)
  98.          msgStr = msgStr + " Arg: " + arg.toString();
  99.       repaint();
  100.       return true;
  101.    }
  102.  
  103.    public void paint(Graphics g)
  104.    {
  105.       FontMetrics currFM = g.getFontMetrics();
  106.       g.drawString(msgStr, ( getSize().width -
  107.       currFM.stringWidth(msgStr)) / 2,
  108.       (getSize().height- currFM.getHeight())/ 2 +
  109.       currFM.getLeading() + currFM.getAscent());
  110.     }
  111.  
  112. }
  113.