import java.awt.*; import java.applet.*; public class DrawImage extends Applet implements Runnable { Thread t; private int width, height; private Graphics buffer; // double buffer private Image bufferImage; // double buffer image private Image myPicture; // a field to store our picture private String message; private int x; // x location of our picture private int y; // y location of our picture public void init() { x = y = 0; message = "Focus Lost"; width=size().width; height=size().height; t=new Thread(this); // create the thread t.start(); // start the thread bufferImage =createImage(width, height); // create off screen image buffer=bufferImage.getGraphics(); // set off screen image to be in the buffer myPicture = getImage(getCodeBase(), "me.gif"); // place our picture into our image object } public void run() { while(true) { repaint(); try{ t.sleep(5); }catch(InterruptedException e) { ; } } } public void paint(Graphics g) { buffer.setColor(Color.red); buffer.fillRect(0,0,width,height); // draw our background rectangle buffer.drawImage(myPicture, x, y, null); // draw our picture into the buffer buffer.setColor(Color.black); buffer.drawString(message, 100,100); g.drawImage(bufferImage, 0, 0, this); // copy buffer into the applet } public void update(Graphics g) { paint(g); } public boolean lostFocus(Event e, Object obj) { message = "Focus Lost"; 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 is " + key; if (key == 1006) x -= 10; if (key == 1007) x+= 10; if (key == 1004) y -= 10; if (key == 1005) y+=10; if (x > width) x = 0; if (x < 0) x = width; if (y > height) y=0; if (y < 0) y = height; return true; } public boolean keyUp(Event e, int key) { message = "Key Up, Key is " + key; return true; } public boolean mouseEnter(Event e, int x, int y) { message = "Mouse has entered the applet."; return true; } public boolean mouseExit(Event e, int x, int y) { message = "Mouse has left the applet."; return true; } public boolean mouseDown(Event e, int x, int y) { message = "Mouse pressed, position x=" + x + " y= " + y; return true; } public boolean mouseUp(Event e, int x, int y) { message = "Mouse released, position x=" + x + " y= " + y; return true; } public boolean mouseMove(Event e, int mousex, int mousey) { message = "Mouse moved, position x=" + x + " y= " + y; x = mousex; y = mousey; return true; } public boolean mouseDrag(Event e, int x, int y) { message = "Mouse dragged, position x=" + x + " y= " + y; return true; } }