矩形不动

时间:2017-09-07 18:59:57

标签: java timer move rectangles

您好我正在尝试创建一个破砖游戏。有一个名为Real_Main的主类和另一个名为Real_Create的类,它扩展了JPanel。我觉得我的代码是正确的,但我似乎无法用左右箭头键移动我的矩形。谁能告诉我它有什么问题?为什么我们需要一个计时器来移动矩形?

import javax.swing.JFrame;
public class Real_Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame jf= new JFrame();

        Real_Create panel=new Real_Create();

        jf.setSize(500, 500);
        jf.setVisible(true);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(panel);

    }

}

Real_Create:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Real_Create extends JPanel implements KeyListener,ActionListener {

     int baseX=200;

    int ballX=250;
    int ballY=250;
    int delay=20;

    Timer tim;


    Boolean play=false;


    public Real_Create(){

        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);

        tim=new Timer(delay,this);

        tim.start();

    }


    public void paint(Graphics g){

        //background
        g.setColor(Color.black);
        g.fillRect(1,1,500,500 );

        //baseplayer

        g.setColor(Color.white);
        g.fillRect(baseX, 430, 80, 10);

        //ball
        g.setColor(Color.green);
        g.fillOval(ballX, ballY, 20, 20);


        g.dispose();




    }



    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        tim.start();
        repaint();

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

        if(e.getKeyCode()== KeyEvent.VK_RIGHT){

            if(baseX >=420){

                baseX=420;
            }

            else{

                moveRight();
            }

        }

         if(e.getKeyCode()== KeyEvent.VK_LEFT){

            if(baseX <=0){

                baseX=0;
            }

            else{

                moveLeft();
            }

        }


    }




    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

public void moveRight(){

        play=true;
        baseX+=10;
    }

    public void moveLeft(){

        play=true;
        baseX-=10;
    }
}

游戏中的图片:

Image from the game

1 个答案:

答案 0 :(得分:0)

由于缺乏线程安全性,您正在处理的是不可预测的Swing行为。根据文档&#34;创建或与Swing组件交互的所有代码必须在事件派发线程上运行&#34;。要实现这一点,您应该像这样重写GUI创建:

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

  private static void createAndShowGUI() {
    JFrame jf = new JFrame();
    Real_Create panel = new Real_Create();
    jf.setSize(500, 500);
    jf.setVisible(true);
    jf.setResizable(false);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(panel);
  }
}

您可以阅读有关主题here的更多信息。