Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame中的运动问题_Java_Eclipse_Swing_Jframe - Fatal编程技术网

Java JFrame中的运动问题

Java JFrame中的运动问题,java,eclipse,swing,jframe,Java,Eclipse,Swing,Jframe,我试图在JFrame中向左或向右移动一个矩形,但当我使用箭头按钮时,该条没有移动;它只是在延伸。长方形是为砖块打破游戏类型 public class main { public static void main(String[] args) { JFrame obj = new JFrame("Brick Breacker"); obj.setBounds(50,50,1200,900); obj.setDefaultCloseOperation(JFram

我试图在
JFrame
中向左或向右移动一个矩形,但当我使用箭头按钮时,该条没有移动;它只是在延伸。长方形是为砖块打破游戏类型

public class main 
{

  public static void main(String[] args)
  {

    JFrame obj = new JFrame("Brick Breacker");
    obj.setBounds(50,50,1200,900);
    obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    obj.setVisible(true);       
    Grafica grafica = new Grafica();
    obj.add(grafica);
  }
}


这不起作用的原因是,您的paint()实现没有清除背景,即您正在多次绘制绿色条-保留已绘制的区域。因此,它看起来像是在拉长而不是移动

您不应该重写
paint(Graphics)
方法,而应该重写
paintComponent(Graphics)
方法,并且应该调用
super.paintComponent(Graphics)

调用super类实现将初始化图形上下文,包括用JPanel的背景色清除它

你可以阅读更多关于如何做的内容

另外,作为旁注,右侧的范围限制有效,但左侧的范围限制无效-它应该检查playerX<0

最后,您实现游戏循环的方式——通过响应关键输入和重新绘制——不是最优的。谷歌搜索“java游戏循环”,了解如何更好地实现这一点(非常简单:游戏循环应独立于输入,并应定期更新场景。然后输入事件应更改游戏状态,并应在下一次场景更新中反映出来)。

阅读。您需要重写paintComponent,而不是paint,并且该方法的实现必须在执行任何其他操作之前调用super.paintComponent。
public class Grafica extends JPanel implements ActionListener , KeyListener
{
    Timer tm = new Timer(0,this); 
    boolean  play = false;
    int playerX = 550; 
    int playerXs = 0;

  public Grafica()
  {
    tm.start();
    addKeyListener(this);
    setFocusable(true); 
    setFocusTraversalKeysEnabled(false);    
  }

  public void paint (Graphics g)
  {                
    //paddle 
     g.setColor(Color.GREEN);
     g.fillRect(playerX, 800, 145, 10);   

     //borders
     g.setColor(Color.BLACK);
     g.fillRect(0, 0, 5, 900);
     g.fillRect(1180, 0, 5, 900);
     g.fillRect(1200, 0, -1200, 5);
     g.setColor(Color.RED);
     g.fillRect(1200, 860, -1200, 5);

     //ball
     g.setColor(Color.blue);         
     g.fillOval(550,700, 26, 26);
  }

  public void actionPerformed(ActionEvent e)
  {     
     playerX = playerX + playerXs;
     repaint();
  }

  public void keyTyped(KeyEvent e) 
  {                 
  }

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

  public void keyPressed(KeyEvent e)
  {
    int c = e.getKeyCode();
    if( c == KeyEvent.VK_RIGHT )
    {
        if(playerX > 850)
        {
            playerX = 850;
        } else 
        {
            moveRight();
        }

    }

    if(c == KeyEvent.VK_LEFT)
    {
        if(playerX > 850)
        {
            playerX = 850;
        } else 
        {
            moveLeft();
        }
     }
  }

  public void moveRight()
  {
     play = true;
     playerX+=20;

  }

  public void moveLeft()
  {
     play = true;
     playerX-=20;

  }
}
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    // Now do your own painting here...
}