Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 7.6</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD> <APPLET CODEBASE="classes" CODE="Applet6.class" WIDTH=500 HEIGHT=100> </APPLET> </TD></TR></TABLE> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.*; public class Applet6 extends Applet { int xPos, yPos; Image butterfly; public void init() { setBackground(Color.white); xPos = this.size().width/2; yPos = this.size().height/2; butterfly = getImage(getCodeBase(),"butterfly.gif"); } public boolean keyDown(Event evt, int x) { int offset; if (evt.shiftDown()) { offset = 20; } else { offset = 10; } switch (x) { case (Event.UP): yPos -= offset; break; case (Event.DOWN): yPos += offset; break; case (Event.LEFT): xPos -= offset; break; case (Event.RIGHT): xPos += offset; break; } repaint(); return true; } public void paint(Graphics g) { g.drawImage(butterfly,xPos,yPos,this); } }