954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Homework Help: How can I get this ball to Move?

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

Mooonky
Newbie Poster
8 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

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

MrScruff
Junior Poster in Training
89 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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.

Mooonky
Newbie Poster
8 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Hi everyone

I think you need to validate and repaint the applet

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

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.

Gink
Light Poster
46 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

//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)

Gink
Light Poster
46 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You