在JFrame中没有看到矩形的Player类

时间:2016-03-24 00:21:28

标签: java swing

我是java的新手,正在尝试创建一个基本的游戏,现在尝试编写播放器类。但是,当我运行游戏时,只会出现JFRame。这些是我的课程,他们没有错误。

SELECT '2015-08-29' < '2015-10-31' AND '2015-11-06' >= '2015-10-01' on_leave;
+----------+
| on_leave |
+----------+
|        1 |
+----------+

1 个答案:

答案 0 :(得分:2)

setVisible(true);并将其移至Game构造函数的最后一位。此外,您应该确保在构建用户界面时在EDT的上下文中工作,有关详细信息,请参阅Initial Threads

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Game extends JFrame {

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

    public Game() {
        setTitle("Game");
        setBackground(Color.WHITE);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel = new GamePanel(this);
        add(panel);
        setResizable(false);
        setSize(WIDTH, HEIGHT);
        setVisible(true);
    }

    public GamePanel getPanel() {
        return panel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new Game();
            }
        });
    }
}

基本上,Swing的布局管理是懒惰的,它不会尝试更新容器层次结构,直到你告诉它(revalidate)或它实现或调整大小,这是一件好事,因为操作可能很昂贵。

接下来,请查看player = new Player(game, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, game.getWidth() - 36);

您将播放器的位置设置为比36组件小width像素,但调用此组件后,width将为0 }。

基本上,您需要允许用户界面“解决”问题。在进行此类呼叫之前。这实际上并不那么容易。您可以使用ComponentListener并监控componentResized事件,但是当窗口首次初始化时,可以多次调整窗口大小,为此,您需要&#34;等待&#34;直到尺寸为#34;已确定&#34;,例如......

public Game() {
    addComponentListener(new ComponentAdapter() {
        private boolean initalised = false;
        private Timer timer = new Timer(250, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initalised = true;
                panel.init();
                timer.stop();
            }
        });

        @Override
        public void componentResized(ComponentEvent e) {
            if (!initalised) {
                timer.restart();
            }
        }
    });

然后,您需要更新GamePanel以提供init方法...

public class GamePanel extends JPanel implements ActionListener, KeyListener {

    public Game game;
    public Player player;

    public GamePanel(Game game) {
        setBackground(Color.GREEN);
        this.game = game;
        addKeyListener(this);
        setFocusable(true);
    }

    public void init() {
        player = new Player(game, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, game.getWidth() - 36);
        repaint();
    }
    //...
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (player != null) {
            player.paint(g);
        }
    }
}

你有各种各样的魔法&#34;数字,不等同于您实际尝试做的事情,例如......

player = new Player(game, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, game.getWidth() - 36);

不应该是game.getWidth() - Player.WIDTH

然后在Player ...

public Player(Game game, int left, int right, int x) {
    this.game = game;
    this.x = x;
    y = game.getHeight() - 20;
    //...
    this.y = y;
    x = game.getWidth() - 36;
}

您将参数x分配给字段x,但是然后在构造函数的末尾更改它?另外,this.y = y毫无意义!?

现在,让我们谈谈一个坏主意setSize(WIDTH, HEIGHT);,这与你的玩家没有出现在你想要的地方的原因有关...

Windows有装饰,所以你的可视区域总是小于实际的窗口大小,更糟糕的是,装饰的大小是可变的。相反,您应该使用JFrame#pack打包窗口的内容,并让内容提供有关它想要的大的提示。

有关详细信息,请查看How can I set in the midst?

您应该使用pack ...

,而不是设置框架的大小
public class Game extends JFrame {

    public GamePanel panel;

    public Game() {
        addComponentListener(new ComponentAdapter() {
            private boolean initalised = false;
            private Timer timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    initalised = true;
                    panel.init();
                    timer.stop();
                }
            });

            @Override
            public void componentResized(ComponentEvent e) {
                if (!initalised) {
                    timer.restart();
                }
            }
        });
        setTitle("Game");
        setBackground(Color.WHITE);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel = new GamePanel(this);
        add(panel);
        setResizable(false);
        pack();
        setVisible(true);
    }

允许GamePanel决定它想成为多大......

public static 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.GREEN);
        this.game = game;
        addKeyListener(this);
        setFocusable(true);
    }

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

这也意味着您应该将Game传递给他们并使用其尺寸,而不是将GamePanel传递给您的对象,例如......

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

您也应该使用KeyListener,而应使用Key Bindings API代替,这样可以解决与焦点相关的问题,并且可以更轻松地配置击键次数

基本上,您错过了一些关于Swing框架如何工作的核心理解,这导致了问题的终结,我花了很多时间来深入了解框架如何正常工作像游戏开发一样复杂

可运行的示例......

现在,因为我基本上已经对你的代码进行了屠杀以使其正常工作......

Happy Player

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 Game extends JFrame {

    public GamePanel panel;

    public Game() {
        addComponentListener(new ComponentAdapter() {
            private boolean initalised = false;
            private Timer timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    initalised = true;
                    panel.init();
                    timer.stop();
                }
            });

            @Override
            public void componentResized(ComponentEvent e) {
                if (!initalised) {
                    timer.restart();
                }
            }
        });
        setTitle("Game");
        setBackground(Color.WHITE);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel = new GamePanel(this);
        add(panel);
        setResizable(false);
        pack();
        setVisible(true);
    }

    public GamePanel getPanel() {
        return panel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new Game();
            }
        });
    }

    public static 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.GREEN);
            this.game = game;
            addKeyListener(this);
            setFocusable(true);
        }

        @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, getWidth() - 50);
            repaint();
        }

        public void update() {
            player.update();
        }

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

    public static 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 x) {
            this.game = game;
            this.left = left;
            this.right = right;
            this.x = x;
            y = game.getHeight() - HEIGHT;
        }

        public void update() {
            if (x > 0 && x < game.getWidth() - WIDTH - 36) {
                x += xa;
            } else if (x == 0) {
                x++;
            } else if (x == game.getWidth() - WIDTH - 36) {
                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);
        }
    }
}