home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / beans / simplebean / example / helpers.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.3 KB  |  89 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 java.awt.*;
  18. import java.applet.*;
  19.  
  20. public class helpers extends Applet
  21. {
  22.    String msgStr;
  23.  
  24.    public void init()
  25.    {
  26.       setFont(new Font("TimesRoman", Font.BOLD, 24));
  27.       msgStr = "Press any key...";
  28.       setForeground(Color.blue);
  29.       add(new Button("make action event"));
  30.       setSize(400,200);
  31.    }
  32.  
  33.    public void start()
  34.    {
  35.       requestFocus();
  36.    }
  37.  
  38.    public boolean keyDown(Event e,  int key)
  39.    {
  40.       msgStr = "\"" + (char)key + "\"" + " key pressed";
  41.       repaint();
  42.       return true;
  43.    }
  44.  
  45.    public boolean keyUp(Event e,  int key)
  46.    {
  47.       msgStr = "\"" + (char)key + "\"" + " key released";
  48.       repaint();
  49.       return true;
  50.    }
  51.  
  52.    public boolean gotFocus(Event e,  Object arg)
  53.    {
  54.       msgStr = "Got Focus";
  55.       if (arg!= null)
  56.          msgStr = msgStr + " arg: " + arg.toString();
  57.       repaint();
  58.       return true;
  59.    }
  60.  
  61.    public boolean lostFocus(Event e,  Object arg)
  62.    {
  63.       msgStr = "Lost Focus" ;
  64.       if (arg != null)
  65.          msgStr = msgStr + " arg: " + arg.toString();
  66.       repaint();
  67.       return true;
  68.    }
  69.  
  70.    public boolean action(Event e,  Object arg)
  71.    {
  72.       msgStr = "Action Event." ;
  73.       if (arg != null)
  74.          msgStr = msgStr + " Arg: " + arg.toString();
  75.       repaint();
  76.       return true;
  77.    }
  78.  
  79.    public void paint(Graphics g)
  80.    {
  81.       FontMetrics currFM = g.getFontMetrics();
  82.       g.drawString(msgStr, ( getSize().width -
  83.       currFM.stringWidth(msgStr)) / 2,
  84.       (getSize().height- currFM.getHeight())/ 2 +
  85.       currFM.getLeading() + currFM.getAscent());
  86.     }
  87.  
  88. }
  89.