multiple animations

i have writen the following program, however i want it to be able to move both walker and walker2 at the same time using the keyboard. right now if walker one is being moved by holding down a key and then walker2 is moved, walker2 overrides walker and walker will stop moving.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Anim1 {
public JFrame f;
public JPanel bottom;
public ImagePanel middle;
public int x=200, y=200, w=0, z=0, v=0, q=0;
public Image [] walker;
public Image [] walker2;
public Image backgroundImage;

class ImagePanel extends JPanel {
public ImagePanel() {
}
public void paintComponent(Graphics g) {
// paint background
super.paintComponent(g);
// paint image in foreground.
g.drawImage(backgroundImage,0,0,this);
g.drawImage(walker[w], x, y, this);
g.drawImage(walker2[q], z, v, this);
}
}

public Anim1(){
f = new JFrame("My First Animation");
bottom = new JPanel();
middle = new ImagePanel();
walker = new Image[8];
walker2 = new Image[8];
walker [0] = Toolkit.getDefaultToolkit().getImage("left.jpg");
walker [1] = Toolkit.getDefaultToolkit().getImage("right.jpg");
walker [2] = Toolkit.getDefaultToolkit().getImage("up.jpg");
walker [3] = Toolkit.getDefaultToolkit().getImage("down.jpg");
walker [4] = Toolkit.getDefaultToolkit().getImage("DownR.jpg");
walker [5] = Toolkit.getDefaultToolkit().getImage("DownL.jpg");
walker [6] = Toolkit.getDefaultToolkit().getImage("UpL.jpg");
walker [7] = Toolkit.getDefaultToolkit().getImage("UpR.jpg");

walker2 [0] = Toolkit.getDefaultToolkit().getImage("left.jpg");
walker2 [1] = Toolkit.getDefaultToolkit().getImage("right.jpg");
walker2 [2] = Toolkit.getDefaultToolkit().getImage("up.jpg");
walker2 [3] = Toolkit.getDefaultToolkit().getImage("down.jpg");
walker2 [4] = Toolkit.getDefaultToolkit().getImage("DownR.jpg");
walker2 [5] = Toolkit.getDefaultToolkit().getImage("DownL.jpg");
walker2 [6] = Toolkit.getDefaultToolkit().getImage("UpL.jpg");
walker2 [7] = Toolkit.getDefaultToolkit().getImage("UpR.jpg");
backgroundImage = Toolkit.getDefaultToolkit().getImage("starfield.gif");
}

public void launchFrame(){
f.addKeyListener(new MyKeyListener());
f.addKeyListener(new MyKeyListener2());
f.setSize(700,600);
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
bottom.setBackground(Color.green);
middle.setBackground(Color.yellow);
f.getContentPane().add(bottom, BorderLayout.SOUTH);
f.getContentPane().add(middle, BorderLayout.CENTER);

f.setVisible(true);
}

public class MyKeyListener implements KeyListener{
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){

if (e.getKeyCode()==KeyEvent.VK_NUMPAD6){
x=x+10;
w=1;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_NUMPAD4){
x=x-10;
w=0;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_NUMPAD8){
y=y-10;
w=2;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_NUMPAD2){
y=y+10;
w=3;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_NUMPAD1){
y=y+10;
x=x-10;
w=5;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_NUMPAD9){
y=y-10;
x=x+10;
w=7;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_NUMPAD3){
y=y+10;
x=x+10;
w=4;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_NUMPAD7){
y=y-10;
x=x-10;
w=6;
middle.repaint();
}
}
}
public class MyKeyListener2 implements KeyListener{
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){

if (e.getKeyCode()==KeyEvent.VK_6){
z=z+10;
q=1;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_4){
z=z-10;
q=0;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_8){
v=v-10;
q=2;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_2){
v=v+10;
q=3;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_1){
v=v+10;
z=z-10;
q=5;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_9){
v=v-10;
z=z+10;
q=7;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_3){
v=v+10;
z=z+10;
q=4;
middle.repaint();
}
if (e.getKeyCode()==KeyEvent.VK_7){
v=v-10;
z=z-10;
q=6;
middle.repaint();
}
}
}
public static void main(String[] args) {
Anim1 a1 = new Anim1();
a1.launchFrame();
}
}

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories