I need the ball to move down until it hits the end of the blue rectangle then move back up until it hits the top and repeat forever. What's wrong with my code?

//This applet draws an animated bouncing ball with uniform speed.
//The ball starts at the center of the graphics page and bounces off
//the top and bottom
 
import java.awt.*;
import java.applet.Applet;
        
public class BouncingBall extends Applet {
    
    //Dimensions of Applet:
    public final int width = 500;
    public final int height = 500;
    //Size and speed of ball:
    public final int diameter = 20;
    public final int speed = 5;
    //Milliseconds to pause between frames:
    public final int pause = 20;
    int y = 250;
    
    public void paint (Graphics page) {
        page.setColor(Color.blue);
        page.fillRect(0,0,500,500);
        page.setColor(Color.red);
        page.fillOval(250 - 10, y - 10, 20, 20);
        //Loop forever;
    }
    public void run ()
    {
        while (true) {
            if (y<=450){
                y--;
                repaint();
            }
            else if (y>=10){
                y++;
                repaint();
            }
            //Pause between frames:
            try { Thread.sleep(pause) ; } catch (Exception e) { };
        }
    }
}

Thanks

What type of errors are you getting or what seems to be the problem? Does it compile and run Ok?

There are no errors. It compiles and puts the picture of the ball in the middle of the blue square, but the ball does not move.

Hi everyone

I think you need to validate and repaint the applet

Richard West

Use repaint() which calls method update(Graphics g), in your case page.
update clears the background of a drawing, then calls method paint which re-draws what you have.

//This applet draws an animated bouncing ball with uniform speed.
//The ball starts at the center of the graphics page and bounces off
//the top and bottom

import java.awt.*;
import java.applet.Applet;

public class BouncingBall extends Applet {

    //Dimensions of Applet:
    public final int width = 500;
    public final int height = 500;
    //Size and speed of ball:
    public final int diameter = 20;
    public final int speed = 5;
    //Milliseconds to pause between frames:
    public final int pause = 20;
    int y = 250;

    public void paint (Graphics page) {
        page.setColor(Color.blue);
        page.fillRect(0,0,500,500);
        page.setColor(Color.red);
        page.fillOval(250 - 10, y - 10, 20, 20);

        {
        while (true) {
            if (y<=450){
                y--;
                repaint();
                update(page);
            }
            else if (y>=10){
                y++;
                repaint();
                update(page);
            }
            //Pause between frames:
            try { Thread.sleep(pause) ; } catch (Exception e) { };
        }
    }
        //Loop forever;
    }
}

you WILL get a stack overflow error when you run this so beware =p(it does run)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.