/* cappello@cs.ucsb.edu Peter Cappello Perm. no. 123456 Zap 'em! Version using inheritance; burn processing in paint method; game initialization done awkwardly */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class zapem extends Applet implements ActionListener, MouseListener { final static int NUMTYPES = 2, // number of kinds of roaches FIELDSIZE = 350, XUL = 25, YUL = 35, XLR = 25 + FIELDSIZE, YLR = 35 + FIELDSIZE, RECTANGLE = 0, OVAL = 1, MAXROACHES = 9, MOUSENOTPRESSED = -1; Label speedL = new Label("Speed: 1 - 5 (Slowest)"), numL = new Label("Number of Roaches: 1 - 9"); TextField speedT = new TextField("5", 1), numT = new TextField("2", 1); int speed = 5, // speed of movements num = 2, // number of roaches numRectangles, numOvals, x, y; // cursor coordinates boolean newGame = true; // first time paint was invoked? Rectangle rectangles[] = new Rectangle[MAXROACHES]; Oval ovals[] = new Oval[MAXROACHES]; public void init() { add(speedL); add(speedT); add(numL); add(numT); speedT.addActionListener(this); numT.addActionListener(this); addMouseListener(this); } public void paint(Graphics g) { // if a new game, initialize the new game if (newGame) { numRectangles = numOvals = 0; x = MOUSENOTPRESSED; // indicate mouse not pressed for (int i = 0; i < num; i++) switch ((int) (Math.random()*NUMTYPES)) { case RECTANGLE: rectangles[numRectangles++] = new Rectangle(FIELDSIZE); break; case OVAL: ovals[numOvals++] = new Oval(FIELDSIZE); break; } newGame = false; } // delineate the field with a black box g.drawRect(XUL, YUL, FIELDSIZE, FIELDSIZE); // if any roach does is not entirely within the field, blacken the field for (int i = 0; i < numRectangles; i++) if (rectangles[i].out(XUL, YUL, XLR, YLR)) { g.fillRect(XUL, YUL, FIELDSIZE, FIELDSIZE); return; } for (int i = 0; i < numOvals; i++) if (ovals[i].out(XUL, YUL, XLR, YLR)) { g.fillRect(XUL, YUL, FIELDSIZE, FIELDSIZE); return; } // if & only if the mouse was pressed, burn all affected roaches if (x != MOUSENOTPRESSED) { // mouse was pressed for (int i = 0; i < numRectangles; i++) if (rectangles[i].in(x, y)) { rectangles[i].setColor(Color.red); rectangles[i].setDx(0); rectangles[i].setDy(0); } for (int i = 0; i < numOvals; i++) if (ovals[i].in(x, y)) { ovals[i].setColor(Color.red); ovals[i].setDx(0); ovals[i].setDy(0); } x = MOUSENOTPRESSED; } // move and draw the roaches for (int i = 0; i < numRectangles; i++) { rectangles[i].move(); rectangles[i].draw(g); } for (int i = 0; i < numOvals; i++) { ovals[i].move(); ovals[i].draw(g); } repaint(speed*100); //repaint after sleeping speed*100 millis } public void actionPerformed(ActionEvent e) { speed = Integer.parseInt(speedT.getText()); if (speed < 1 || speed > 5) { speedT.setText("5"); speed = 5; } num = Integer.parseInt(numT.getText()); if (num < 1 || num > 9) { numT.setText("2"); num = 2; } newGame = true; repaint(); } public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } public void mouseClicked(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }