/** Etch.java David Reed 4/15/02 ** ** Etch class: for simulating an Etch-A-Sketch **********************************************/ import java.awt.*; import java.applet.*; public class Etch extends Applet { private int WIDTH, HEIGHT, STEP, currentX, currentY; private Image offScreenImage; private Graphics offScreenGraphics; public void init() // gets the applet dimensions and the step size, as well as // initializing the X and Y coordinates to the center of the applet { Dimension dim = getSize(); WIDTH = dim.width; HEIGHT = dim.height; STEP = Integer.parseInt(getParameter("stepSize")); currentX = WIDTH/2; currentY = HEIGHT/2; offScreenImage = createImage(WIDTH, HEIGHT); offScreenGraphics = offScreenImage.getGraphics(); } public void move(String dir) // moves the drawing pen according to the specified dir, // updating currentX and currentY in the process { int oldX = currentX, oldY = currentY; if (dir.equals("up")) { currentY -= STEP; } else if (dir.equals("down")) { currentY += STEP; } else if (dir.equals("left")) { currentX -= STEP; } else if (dir.equals("right")) { currentX += STEP; } offScreenGraphics.drawLine(oldX, oldY, currentX, currentY); paint(getGraphics()); } public void paint(Graphics g) { g.drawImage(offScreenImage, 0, 0, null); } }