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