import java.applet.*; import java.awt.*; public class Keys extends Applet implements Runnable { Thread t; private Image bufferImage; private Graphics buffer; // create a graphics buffer and off screen image private int width; private int height; private String message; public void init() { setBackground(Color.black); t=new Thread(this); t.start(); width=size().width; height=size().height; message = "Focus Lost"; bufferImage=createImage(width, height); buffer = bufferImage.getGraphics(); // create off screen buffer image and place it in the buffer } public void run() { while(true) { repaint(); try{ t.sleep(5); }catch(InterruptedException e){ ; } } } public void paint(Graphics g) { if (buffer == null) { // make sure the buffer was setup correctly bufferImage = createImage(width, height); buffer = bufferImage.getGraphics(); } buffer.setColor(Color.black); buffer.fillRect(0,0, width, height); // draw our background square to the buffer buffer.setColor(Color.red); buffer.drawString(message, 100, 100); g.drawImage(bufferImage, 0, 0, this); // draw our buffer image to the applet } public void update(Graphics g) { paint(g); } public boolean lostFocus(Event e, Object obj) { message = "Lost Focus"; return super.lostFocus(e, obj); } public boolean gotFocus(Event e, Object obj) { message = "Got Focus"; return super.gotFocus(e, obj); } public boolean keyDown(Event e, int key) { message = "Key Down = " + key; return true; } public boolean keyUp(Event e, int key) { message = "Key Up = " + key; return true; } }