KeyMover
Retired DISLab
import acm.program.*; import acm.graphics.*; import java.awt.event.*; public class KeyMover extends GraphicsProgram { private GOval theCircle; public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_W) { theCircle.move(0, -10); } else if (e.getKeyCode() == KeyEvent.VK_S) { theCircle.move(0, 10); } else if (e.getKeyCode() == KeyEvent.VK_A) { theCircle.move(-10, 0); } else if (e.getKeyCode() == KeyEvent.VK_D) { theCircle.move(10, 0); } } public void run() { theCircle = new GOval(150,150); this.add(theCircle,100,100); addKeyListeners(); } public static void main(String[] args) { String[] newArgs = new String[args.length + 1]; System.arraycopy(args, 0, newArgs, 0, args.length); newArgs[args.length] = "code=" + new SecurityManager(){ public String className() { return this.getClassContext()[1].getCanonicalName(); } }.className(); GraphicsProgram.main(newArgs); } }