我如何使这个框架中的圆实际上自动移动



我无法解决这个问题。我已经用我想要的输出创建了这个框架和面板。唯一的问题是,我一直无法弄清楚如何使球"自动"移动。我理想的游戏是在游戏开始时将球/圆圈掉落,也许只需单击按钮等。我将如何做到这一点?

我尝试使用 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 ( );
}
}

我希望输出在游戏开始/按下按钮时将球下降到蓝色矩形上,但我无法让它工作。

由于你想自动移动球,在run()方法中用一些增量值改变球的位置(你的xpos,ypos,xpos1和ypos2)(就像你在keyPressed()接口方法中所做的那样)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 就会更新一次。

相关内容

最新更新