我该如何做才能使此框中的圆真正自动移动?

时间:2018-12-31 09:51:26

标签: java swing animation awt

我无法解决这个问题。我用所需的输出创建了该框架和面板。唯一的事情是我无法弄清楚如何使球“自动”移动。我理想的游戏可能是在游戏开始时就放下了球/圆,可能是单击了按钮或类似的按钮。我将如何去做呢?

我试图用E键移动球,但是这对于用户来说太不方便了,因此我认为没有事件处理程序的情况下移动球是更好的选择。

    private int ballX, ballY, ballR;
private int score1, score2;
private JPanel panel;
private JFrame frame;
private DrawingArea canvas;
private int xpos, ypos,width,height;
private int xpos2, ypos2, width2, height2;
public Pong(){
    xpos = 300;
    ypos = 550;
    width = 100;
    height = 50;

    xpos2 = 300;
    ypos2 = 100;
    width2 = 100;
    height2 = 50;

    ballR = 50;
    ballX = 325; 
    ballY = 330;


}
public static void main(String[]args){
    Pong p = new Pong();
    p.run();
}
public void run(){
    frame = new JFrame("Pong");
    frame.setSize(700,700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas = new DrawingArea();     // create a panel to draw on
    canvas.setBackground(Color.WHITE);
    canvas.addFocusListener(this);
    canvas.addKeyListener(this);
    canvas.addMouseListener(this);


    frame.getContentPane().add(canvas);
    frame.setVisible(true);
}
class DrawingArea extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor ( Color.blue );
        g.fillRect ( xpos, ypos, width, height );
        g.setColor(Color.red);
        g.fillRect(xpos2, ypos2, width2, height2);


        g.setColor(Color.black);
        g.fillRect(0,0,700,50);
        g.fillRect(0,630,700,50);

        g.fillOval(ballX,ballY,ballR, ballR);

    }
}
public void keyPressed ( KeyEvent e )    {
    int value = e.getKeyCode();

    switch ( value )    {
        case KeyEvent.VK_RIGHT:     xpos += 50; break;
        case KeyEvent.VK_LEFT:      xpos -= 50; break;
        case KeyEvent.VK_A:         xpos2 -= 50; break;
        case KeyEvent.VK_D:         xpos2 += 50; break;
        /*try to drop the ball with the space button
         * case KeyEvent.VK_SPACE:
            ballX+=25;
            ballY+=25;
            break;
        case KeyEvent.VK_ENTER:
            xpos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
            ypos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
            break; 
        */
    }
    if( (xpos < 0 || xpos >= 700) || (xpos2 < 0 || xpos2 >= 700)){
         if(xpos < 0 || xpos2 < 0){
            if(xpos < 0) xpos = 0;
            else if(xpos2 < 0) xpos2 = 0;
            return;
        }
        else if(xpos >= 700 || xpos2 >= 700){
            if(xpos >= 700)xpos = 550;
            else if(xpos2 >= 700) xpos2=550;
            return;
        }
    }

    canvas.repaint ( );
}

}

我希望输出随着游戏开始/按下按钮而使球下降到蓝色矩形上,但是我无法使它起作用。

1 个答案:

答案 0 :(得分:0)

要自动移动球,请在run()方法中使用一些增量值(如在keyPressed()接口方法中所做的那样)更改球的位置(您的xpos,ypos,xpos1和ypos2)dx, dy等,然后调用JFrame的repaint()方法。

public class BouncingBall extends JPanel {

  // Box height and width
  int width;
  int height;

  // Ball Size
  float radius = 40; 
  float diameter = radius * 2;

  // Center of Call
  float X = radius + 50;
  float Y = radius + 20;

  // Direction
  float dx = 3;
  float dy = 3;

  public BouncingBall() {

    Thread thread = new Thread() {
      public void run() {
        while (true) {

          width = getWidth();
          height = getHeight();

          X = X + dx ;
          Y = Y + dy;

          if (X - radius < 0) {
            dx = -dx; 
            X = radius; 
          } else if (X + radius > width) {
            dx = -dx;
            X = width - radius;
          }

          if (Y - radius < 0) {
            dy = -dy;
            Y = radius;
          } else if (Y + radius > height) {
            dy = -dy;
            Y = height - radius;
          }
          repaint();

          try {
            Thread.sleep(50);
          } catch (InterruptedException ex) {
          }

        }
      }
    };
    thread.start();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
  }

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Bouncing Ball");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setContentPane(new BouncingBall());
    frame.setVisible(true);
  }
}

由于我在Thread.sleep()中使用了50毫秒的渲染时间,因此您可以指定自己的时间。因此,对于我来说,每50毫秒您的JFrame都会更新一次。