Home » Java » Calling Graphics From Another Class

Calling Graphics From Another Class

Posted by: admin June 30, 2018 Leave a comment

Questions:

I’m trying to create a two player game, that uses keyboard input to move the players. So far when I test it from it’s own class it works and I can move the players but when I call it from my game class it doesn’t work. I’m also having this problem with graphics in another class as well. They show up but don’t move.
I’m not sure if I’m calling it wrong or if there is something I need to add. I think it might have to do with how I repaint it but I’m not sure.

Code where I paint the different graphics( this is in my game class):

     public void paintComponent(Graphics g) {
        super.paintComponent(g);
        aa.begin(g, -450);
        aa.Menu(g);
        if (running == true){
            g.clearRect(0, 0, getWidth(), getHeight());
            one.healthbar(g);
            two.healthbar(g);
            one.drawplayer(g);
            two.drawplayer(g);
            aa.timer(g,0);
        }
     }

   @Override
     public void actionPerformed(ActionEvent e) {
         //running = true;
         //Run();  
     }

Code for One of my Player Class:

public class PlayerOne extends JPanel implements ActionListener{
private int x = 10, y = 10, velx = 0, vely = 0;
public static int healthOne = 200;
int power = 5;
Timer tm = new Timer(5, this);
boolean playing = true;

public PlayerOne(){
    tm.start();
    InputMap im = getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = getActionMap();

    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "W.pressed");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), "W.released");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "S.pressed");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), "S.released");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "A.pressed");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "A.released");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "D.pressed");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "D.released");

    am.put("W.pressed", new MoveAction(-1, 0));
    am.put("W.released", new MoveAction(0, 0));
    am.put("S.pressed", new MoveAction(1, 0));
    am.put("S.released", new MoveAction(0, 0));
    am.put("A.pressed", new MoveAction(0, -1));
    am.put("A.released", new MoveAction(0, 0));
    am.put("D.pressed", new MoveAction(0, 1));
    am.put("D.released", new MoveAction(0, 0));  
}
//@Override
/*public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillRect(x, y, 30, 30);
    healthbar(g);
}*/
public void drawplayer (Graphics g){
    g.setColor(Color.BLUE);
    g.fillRect(x, y, 30, 30);
}

public void healthbar (Graphics g){

    g.setColor(Color.GREEN);
    g.fillRect(0, 640, healthOne, 25);
    //health -=20;
    repaint();

}

public class MoveAction extends AbstractAction {

    private int yDelta;
    private int xDelta;

    public MoveAction(int yDelta, int xDelta) {
        this.yDelta = yDelta;
        this.xDelta = xDelta;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        vely = yDelta;
        velx = xDelta;
    }
}    
@Override
public void actionPerformed(ActionEvent e) {
    repaint();
    x += velx;
    y += vely;

}

}

Answers: