//------------FROM THE OTHER import java.awt.*; import java.awt.event.*; import java.applet.Applet; import javax.swing.JApplet; /** * * @author Mr. Merlis * @version June 2006 */ public class WalkerApplet extends JApplet implements KeyListener , FocusListener, MouseListener { // instance variables - replace the example below with your own // private int x=400; private Walker sleepy; private Walker tank2; private int qPressed = 0; private int sPressed = 0; private int moveAmt = 20; Graphics2D g; /** * Called by the browser or applet viewer to inform this Applet that it * has been loaded into the system. It is always called before the first * time that the start method is called. */ public void init() { // provide any initialisation necessary for your Applet addKeyListener(this); } /** * Called by the browser or applet viewer to inform this Applet that it * should start its execution. It is called after the init method and * each time the Applet is revisited in a Web page. */ public void start() { setVisible (true); g = (Graphics2D)getGraphics(); sleepy =new Walker(g,200,getSize().width, getSize().height, 16, 500, 430); tank2 =new Walker(g,200,getSize().width, getSize().height, 8, 200, 200); paint(g); // g.drawString("Push Q to quit MY BOY.", 1, 15); //tells them to push q to quit while(qPressed != 1 && false) //while the person has not pushed Q or P { paint(g); pause(100); g.drawString("Push Q to quit.", 100, 15); //tells them to push q to quit } } public void keyTyped(KeyEvent evt) { // The user has typed a character, while the // apple thas the input focus. If it is one // of the keys that represents a color, change // the color of the square and redraw the applet. char ch = evt.getKeyChar(); if(ch == 'Q' || ch == 'q') { qPressed = 1; } else if(ch == 'S' || ch == 's') { sPressed = 1; } else if(ch == 'P' || ch == 'p') { qPressed = 2; } } // end keyTyped() /** * This may be the most important method in your applet: Here, the * drawing of the applet gets done. "paint" gets called everytime the * applet should be drawn on the screen. So put the code here that * shows the applet. * * @param g the Graphics object for this applet */ public boolean paint(Graphics2D g) { sleepy.drawBackground(); // clear area // tank2.draw(); return sleepy.draw(); } /** * Used between drawing the different Walkers to provide * animation */ private static void pause (int wait) { long timeToQuit = System.currentTimeMillis() + wait; while (System.currentTimeMillis() < timeToQuit) { } // take no action } //====================== public void keyPressed(KeyEvent evt) { // Called when the user has pressed a key, which can be // a special key such as an arrow key. If the key pressed // was one of the arrow keys, move the square (but make sure // that it doesn't move off the edge, allowing for a // 3-pixel border all around the applet). paint(g); int key = evt.getKeyCode(); // keyboard code for the key that was pressed if (key == KeyEvent.VK_LEFT) //if the player presses the left arrow key { sleepy.changeXLoc(-1); } else if (key == KeyEvent.VK_RIGHT) //if the player presses the right arrow key { sleepy.changeXLoc(1); } else if (key == KeyEvent.VK_DOWN) //if the player presses the down arrow key { sleepy.changeGunAngle(-1); } else if (key == KeyEvent.VK_UP) //if the player presses the up arrow key { sleepy.changeGunAngle(1); } else if (key == KeyEvent.VK_F) //if the player presses the up arrow key { sleepy.drawGunShot(); } else if (key == KeyEvent.VK_N) //if the player presses the up arrow key { sleepy.changeHeadAngle(1); } else if (key == KeyEvent.VK_M) //if the player presses the up arrow key { sleepy.changeHeadAngle(-1); } } // end keyPressed() public void keyReleased(KeyEvent evt) { // empty method, required by the KeyListener Interface } public void mousePressed(MouseEvent evt) { // Request the input focus when the user clicks on // the applet. (On most platforms, there is no need // for this. However, Sun's own Java implementation // requires it.) requestFocus(); } public void focusGained(FocusEvent evt) { // The applet now has the input focus. //repaint(); // redraw with cyan border } public void focusLost(FocusEvent evt) { // The applet has now lost the input focus. //focussed = false; //repaint(); // redraw without cyan border } public void mouseEntered(MouseEvent evt) { } // Required by the public void mouseExited(MouseEvent evt) { } // MouseListener public void mouseReleased(MouseEvent evt) { } // interface. public void mouseClicked(MouseEvent evt) { } }