Hello,

The class BouncingBall makes the ball move around a box bouncing back and forth when it hits the "wall". My problems is that it moves too fast. Is there a way to make it move a little bit slower?

Thanks for your help.

Code:
public class BouncingBall extends SelfDrawingBall
{
    int dx = 20;
    int dy = 17;
    
    public BouncingBall()
    {
        super();
    }

   public void bounce()
   {
       for (int i=0; i<500; i++)
       {
           if ((xPosition<0) || (xPosition + diameter)>300)
           dx = -dx;
           if ((yPosition<0) || (yPosition + diameter)>300)
           dy = -dy;
           super.move(dx,dy);
           super.draw();
        }
   }
}
Code:
public class SelfDrawingBall extends Ball
{
    public SelfDrawingBall  ()
      {
       super ();
       }
     
    protected void draw()
       {
         Canvas myCanvas = Canvas.getCanvas();
         myCanvas.draw(this, colour, new Ellipse2D.Double(xPosition, yPosition, 
                                                          diameter, diameter));
        }
}
Code:
public class Ball
{
    protected int diameter;
    protected int xPosition;
    protected int yPosition;
    protected String colour;
    
    public Ball()
    {
        diameter = 30;
        xPosition = 100;
        yPosition = 100;
        colour = "red";
    }

    public Ball(int diam)
    {
        diameter = diam;
        xPosition = 100;
        yPosition = 100;
        colour = "red";
    }

    public void blowUp ()
      {
          diameter = diameter + 10;
      }
       
  public void move (int dx, int dy)
    {
       xPosition = xPosition + dx;
       yPosition = yPosition + dy;
     }
       
 public int getX ()
    {
      return xPosition;
    }
    
public int getY ()
    {
      return yPosition;
    }
    
public void setX (int newX)
    {
      xPosition = newX;
    }
 
public void setY (int newY)
   {
     yPosition = newY;
   }
      
public Shape shape()
    {
        return new java.awt.geom.Ellipse2D.Float(diameter, diameter, xPosition, yPosition);
    }
}