Key
   

 

This is a demonstrations for the user-computer interaction. When you type in the letter using the keyboard, the letter is displayed on the screen.

Web Configuration:

<applet code = "Keys.class" width=100 height=100>
</applet>

Source Code:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;

public class Keys extends java.applet.Applet {

     char currkey;
     int currx;
     int curry;

     /** Initializes the applet Keys */
     public void init () {
          setLayout (new java.awt.BorderLayout ());
          currx = (getSize().width / 2) -8;
          curry = (getSize().height /2) -32;
          setBackground(Color.white);
          setFont (new Font ("Helvetica", Font.BOLD, 36));
          requestFocus();
      }

      public boolean keyDown (Event eve, int key){
           switch (key) {
           case Event.DOWN:
                 curry +=5;
                 break;
           case Event.UP:
                 curry -=5;
                 break;
           case Event.LEFT:
                 currx -=5;
                 break;
           case Event.RIGHT:
                 currx +=5;
                 break;
           default:
                 currkey = (char)key;
            }
            repaint ();
            return true;
      }

      public void paint(Graphics g){
           if (currkey != 0){
                  g.drawString(String.valueOf(currkey),currx, curry);
           }
       }
}

Copyright©2002, All Right Reserved

Department of Geography, San Diego State University