从另一个班级调用图形

时间:2018-05-22 15:31:14

标签: java graphics repaint

我试图创建一个双人游戏,使用键盘输入来移动玩家。到目前为止,当我从它自己的班级测试时,它可以工作,我可以移动玩家但是当我从我的游戏课程中调用它时它不起作用。我也在另一个类中遇到图形问题。他们出现但不动。 我不确定我是否说错了,或者我是否需要添加一些内容。我认为这可能与我如何重绘它有关,但我不确定。

我绘制不同图形的代码(这是我的游戏类):

     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();  
     }

我的其中一个玩家类的代码:

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;

    }
}

0 个答案:

没有答案