import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class Picture extends Applet implements KeyListener { private Image sonic[] = new Image[22]; private Image bill; private int billX = 200, billY = 300; private int X = 0, Y = 300; /* Initializing the variables for the X, and Y coordinates of the images*/ int i = 0; int s = 1; int t = 0; int facing = 0; // 0 is facing right, and 1 is facing left. public void init() { sonic[0] = getImage(getDocumentBase(), "Sonic_Static_Right.gif"); // Sonic facing right sonic[1] = getImage(getDocumentBase(), "Sonic_Static_Left.gif"); // Sonic facing left sonic[2] = getImage(getDocumentBase(), "Sonic_Run_Right_1.gif"); /////////////////////////////////// sonic[3] = getImage(getDocumentBase(), "Sonic_Run_Right_2.gif"); /////////////////////////////////// sonic[4] = getImage(getDocumentBase(), "Sonic_Run_Right_3.gif"); /////////////////////////////////// sonic[5] = getImage(getDocumentBase(), "Sonic_Run_Right_4.gif"); /////////Sonic Running Right/////// sonic[6] = getImage(getDocumentBase(), "Sonic_Run_Right_5.gif"); /////////////////////////////////// sonic[7] = getImage(getDocumentBase(), "Sonic_Run_Right_6.gif"); /////////////////////////////////// sonic[8] = getImage(getDocumentBase(), "Sonic_Run_Right_7.gif"); /////////////////////////////////// sonic[9] = getImage(getDocumentBase(), "Sonic_Run_Right_8.gif"); /////////////////////////////////// sonic[10] = getImage(getDocumentBase(), "Sonic_Run_Left_1.gif"); /////////////////////////////////// sonic[11] = getImage(getDocumentBase(), "Sonic_Run_Left_2.gif"); /////////////////////////////////// sonic[12] = getImage(getDocumentBase(), "Sonic_Run_Left_3.gif"); /////////////////////////////////// sonic[13] = getImage(getDocumentBase(), "Sonic_Run_Left_4.gif"); /////////Sonic Running Left/////// sonic[14] = getImage(getDocumentBase(), "Sonic_Run_Left_5.gif"); /////////////////////////////////// sonic[15] = getImage(getDocumentBase(), "Sonic_Run_Left_6.gif"); /////////////////////////////////// sonic[16] = getImage(getDocumentBase(), "Sonic_Run_Left_7.gif"); /////////////////////////////////// sonic[17] = getImage(getDocumentBase(), "Sonic_Run_Left_8.gif"); /////////////////////////////////// sonic[18] = getImage(getDocumentBase(), "Sonic_Right_Duck.gif"); sonic[19] = getImage(getDocumentBase(), "Sonic_Left_Duck.gif"); sonic[20] = getImage(getDocumentBase(), "Sonic_Right_Look.gif"); sonic[21] = getImage(getDocumentBase(), "Sonic_Left_Look.gif"); /////////////////////////////////////////////////////////////////// ///////////////////////////End Sonic Images//////////////////////// bill = getImage(getDocumentBase(), "Bill_Man.gif");// Bill Gates standing still addKeyListener(this); requestFocus(); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_LEFT) { X = X - 5; if(s == 1) { i = 10; } if(t % 2 == 0) { if(i == 17) { i = 10; } s++; i++; } t++; } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { X = X + 5; if(s == 1) { i = 2; } if(t % 2 == 0) { if(i == 9) { i = 2; } s++; i++; } t++; } if(e.getKeyCode() == KeyEvent.VK_DOWN) { if (facing == 0) i = 18; if (facing == 1) i = 19; } if(e.getKeyCode() == KeyEvent.VK_UP) { if (facing == 0) i = 20; if (facing == 1) i = 21; } repaint(); } public void keyReleased(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_RIGHT) { i = 0; t = 0; s = 1; facing = 0; repaint(); } if(e.getKeyCode() == KeyEvent.VK_LEFT) { i = 1; t = 0; s = 1; facing = 1; repaint(); } if(e.getKeyCode() == KeyEvent.VK_UP) { if (facing == 0) i = 0; if (facing == 1) i = 1; repaint(); } if(e.getKeyCode() == KeyEvent.VK_DOWN) { if (facing == 0) i = 0; if (facing == 1) i = 1; repaint(); } } public void paint(Graphics g) { g.fillRect(0,350, 1500, 400); if(X==billX-10) g.drawImage(sonic[i], billX-10, Y, 40, 50, this); else g.drawImage(sonic[i], X, Y, 40, 50, this); g.drawImage(bill, billX, billY, 33, 51, this); } }