为什么我不能使用Java中的导航键移动矩形?

时间:2016-03-24 22:50:57

标签: java swing awt keylistener

嘿伙计们,当我向左或向右碰时,我试图让我的Player类移动但没有任何反应/我是java的新手,并且真的不明白为什么它不起作用。我没有错,但仍然没有任何反应。谢谢你的帮助。另外,如果你能向我解释为什么它不能正常工作那么真有用。

import java.awt.Color;
        import java.awt.Dimension;
        import java.awt.EventQueue;
        import java.awt.Graphics;
        import java.awt.Rectangle;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import java.awt.event.ComponentAdapter;
        import java.awt.event.ComponentEvent;
        import java.awt.event.KeyEvent;
        import java.awt.event.KeyListener;
        import javax.swing.JFrame;
        import static javax.swing.JFrame.EXIT_ON_CLOSE;
        import javax.swing.JPanel;
        import javax.swing.Timer;
        import javax.swing.UIManager;
        import javax.swing.UnsupportedLookAndFeelException;

        public class GamePanel extends JPanel implements ActionListener, KeyListener {

            public final static int WIDTH = 700, HEIGHT = 450;

            public Game game;
            public Player player;

            public GamePanel(Game game) {
                setBackground(Color.WHITE);
                this.game = game;
                addKeyListener(this);
                setFocusable(true);
            }
                    public void update() {
                player.update();
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(WIDTH, HEIGHT);
            }

            public void init() {
                System.out.println("!!");
                player = new Player(this, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, getHeight() - 50, getWidth() / 2);
                repaint();
            }


            public void actionPerformed(ActionEvent e) {
                update();
                repaint();
            }

            public Player getPlayer(int playerNo) {
                return player;
            }
      public void keyPressed(KeyEvent e) {
            player.pressed(e.getKeyCode());
        }

        public void keyReleased(KeyEvent e) {
            player.released(e.getKeyCode());
        }

        public void keyTyped(KeyEvent e) {
    ;
        }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (player != null) {
                    player.paint(g);
                }
            }
        }

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Player {

        public static final int WIDTH = 50, HEIGHT = 50;
        public GamePanel game;
        public int left, right;
        public int y;
        public int x, xa;

        public Player(GamePanel game, int left, int right, int y, int x) {
            this.game = game;
            this.left = left;
            this.right = right;
            this.x = x;
            y = game.getHeight() - HEIGHT;
            x = game.getWidth() / 2;
        }

        public void update() {
            if (x > 0 && x < game.getWidth() - WIDTH) {
                x += xa;
            } else if (x == 0) {
                x++;
            } else if (x == game.getWidth() - WIDTH) {
                x--;
            }
        }

 public void pressed(int keyCode) {
        if (keyCode == left)
            xa = -1;
        else if (keyCode == right)
            xa = 1;
    }

    public void released(int keyCode) {
        if (keyCode == left || keyCode == right)
            xa = 0;
    }

        public Rectangle getBounds() {
            return new Rectangle(x, y, WIDTH, HEIGHT);
        }

        public void paint(Graphics g) {
            System.out.println(x + "x" + y);
            g.fillRect(x, y, WIDTH, HEIGHT);
            g.setColor(Color.ORANGE);
        }
    }

3 个答案:

答案 0 :(得分:0)

您在actionPerformed回调中有更新和重绘逻辑,而KeyPressed为玩家设置移动变量。在侦听器方法中调试或打印某些内容,以查看调用方式。我不认为actionPerformed和KeyPressed将按照你期望的方式和顺序调用。

答案 1 :(得分:0)

您只需要在 keyReleased()方法的末尾添加repaint();语句。无论您是在 keyTyped()还是 keyPressed()方法中执行某些操作,只要您释放 keyReleased()方法,键。所以程序控件应该在这里遇到repaint()语句。

@Override
public void keyReleased(KeyEvent e){
    player.released(e.getKeyCode());
    repaint(); //<--- THIS HERE
}

我希望这能解决你的问题。

请注意@SasonOhanian的回答并改进您的代码。

答案 2 :(得分:0)

Ty所有的帮助,但我需要使x = x + 1和x = x -1