如何在边界之间反弹图像

How to bounce an image between the boundaries

我正试图在 window 的边界之间弹跳球图像。然而,当球到达顶部边界时,它不会反弹回来。希望有人能帮我解决这个问题。

这是我的代码:

    import java.awt.*;
    import java.applet.*;
    public class Ball {
    private int x=355 ;
    private int y=500;
    private int xVel = -3;
    private int yVel = 3;

    private Image ball;

    public Ball (Breakout bR){

        ball = bR.getImage(bR.getDocumentBase(),"ball.png");


    }
    public void update(Breakout bR){
       x += xVel;
       y += yVel;
       if (x < 0){
           xVel = 3;
        }
       else if (x > bR.getWidth()){
            xVel = -3;
        }
       if(y > bR.getHeight()){
           yVel = -3;
        }
       else if (y < 0){
            xVel = 3;
        }
    }

    public void paint (Graphics g, Breakout bR){
        g.drawImage(ball,x,y,bR);

    }
}

Thanks for your help :)   

你在最后一个 else-if... 中有错字,应该是

   else if (y < 0){
        yVel = 3;
    }