简体   繁体   English

形状在绘图面板Java上不移动

[英]Shapes not moving on drawing panel java

i am trying to make a simple java game with a bat(paddle) and ball. 我试图用蝙蝠(桨)和球做一个简单的Java游戏。 So far i have painted the 2 objects onto the panel, however i cant get them to move. 到目前为止,我已经将2个对象绘制到面板上,但是我无法移动它们。 i have added key events for the bat and a move() method for the ball. 我为蝙蝠添加了关键事件,为球添加了move()方法。 Below are all my classes. 以下是我所有的课程。

Game class: 游戏类别:

public class Game extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Game();
            }});
    }

    MyDrawingPanel myDrawingPanel = new MyDrawingPanel(this);
    MyUIPanel myUIPanel = new MyUIPanel(this);


    public Game()
    {
        setSize(1160,660); // you may change frame and panel sizes
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container cp = getContentPane();    
        cp.setLayout(new FlowLayout());
        cp.add(myDrawingPanel);
        cp.add(myUIPanel);

        setVisible(true);
    }
}

MyDrawingPanel class: MyDrawingPanel类:

class MyDrawingPanel extends JPanel {

    Game game;

    Ball ball = new Ball(this);
    Bat bat = new Bat(this);

    public MyDrawingPanel(Game game)
    {
        this.game=game;
        setPreferredSize(new Dimension(800,600));
        setBackground(Color.RED);
        requestFocus();
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        ball.paint(g2d);

        Graphics2D gBat = (Graphics2D) g;
        bat.paint(gBat);
    }
}

Ball class: 球类:

public class Ball {

    int x = 0;
    int y = 0;
    int xa = 1;
    int ya = 1;
    private MyDrawingPanel myDrawingPanel;

    public Ball(MyDrawingPanel myDrawingPanel) {
        this.myDrawingPanel = myDrawingPanel;
    }

    public void move() {
        if (x + xa < 0)
            xa = 1;
        if (x + xa > myDrawingPanel.getWidth() - 30)
            xa = -1;
        if (y + ya < 0)
            ya = 1;
        if (y + ya > myDrawingPanel.getHeight() - 30)
            ya = -1;

        x = x + xa;
        y = y + ya;
    }

    public void paint(Graphics2D  g) {
        g.fillOval(x, y, 30, 30);
    }
}

Bat class: 蝙蝠班:

public class Bat{

    int x = 0;
    int xa = 0;
    private MyDrawingPanel myDrawingPanel;

    public Bat(MyDrawingPanel myDrawingPanel)
    {
        this.myDrawingPanel = myDrawingPanel;
    }

    public void move(){
        if(x + xa > 0 && x + xa <myDrawingPanel.getWidth()-60 )
            x = x + xa;
    }

    public void paint(Graphics2D g)
    {
        g.setColor(Color.BLUE);
        g.fillRoundRect(x, 500, 100, 20, 10, 10);
    }

    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == KeyEvent.VK_LEFT)
            xa = -1;
        if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            xa = 1;
    }

    public void keyReleased(KeyEvent e)
    {
        xa = 0;
    }
}

Methods such as keyPressed and keyReleased don't do anything without a KeyListener implementation. 没有KeyListener实现,诸如keyPressedkeyReleased方法不会做任何事情。 So currently, your methods are useless. 因此,当前,您的方法无用。 What you should do have the DrawingPanel class implement the KeyListener , like this 您应该使DrawingPanel类实现KeyListener是这样的

public class DrawingPanel extends JPanel implements KeyListener {
    ...
}

The methods, keyPressed and keyReleased should be in that class. 方法keyPressedkeyReleased应该在该类中。 You'll also want setter methods for what ever variables are updated in the Bat class, like x and xa , is those are the variables that determine the movement of the Bat . 您还将希望使用setter方法来对Bat类中的变量(例如xxa进行更新,这些变量是确定Bat运动的变量。

So in the keyPressed , which should be in the DrawingPanel class, it could look something like this 因此,在应该位于DrawingPanel类中的keyPressed中,它可能看起来像这样

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        bat.setXa(bat.getXa() - 1);
        repaint();
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        bat.setXa(1);
        repaint();
    }
}

Notice how I call repaint() . 注意我如何调用repaint() That's what you need to do after you move something. 那是您移动某些东西后需要做的。

This should get you started in the right direction 这应该使您朝正确的方向开始


EDIT 编辑

Forget the majority of the above answer. 忘记上面的大多数答案。

Instead of using a KeyListener though I would recommeng using key binding. 虽然我会推荐使用键绑定,但是不要使用KeyListener You may face focus problems using KeyListener . 使用KeyListener可能会遇到焦点问题。 I implemented the keybinding for the DrawingPanel and it works fine. 我为DrawingPanel实现了键盘绑定,并且工作正常。 It'll give you some ideas to work with. 它会给您一些想法。

 class MyDrawingPanel extends JPanel { Game game; Ball ball = new Ball(this); Bat bat = new Bat(this); public MyDrawingPanel(Game game) { this.game = game; setPreferredSize(new Dimension(800, 600)); setBackground(Color.RED); requestFocus(); Action rightAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { bat.x += 10; repaint(); } }; Action leftAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { bat.x -= 10; repaint(); } }; InputMap inputMap = getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = getActionMap(); inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "rightAction"); actionMap.put("rightAction", rightAction); inputMap.put(KeyStroke.getKeyStroke("LEFT"), "leftAction"); actionMap.put("leftAction", leftAction); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ball.paint(g2d); Graphics2D gBat = (Graphics2D) g; bat.paint(gBat); } } 

See How to use key bindings 请参阅如何使用键绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM