0

My program is to create a bouncing ball that goes up and down and up minus 30% each time and down... in tell the ball has stopped in a resting position.

also I would like to make the ball progressively slow down as it reaches the top of the bonce, and progressively speed up as it descends back to its original position.

So I got the first part set up, I'm just having trouble with not making an infinity loop, and decreasing the up wards y stopping position to decrease by 30% after each bounce.

As I was writing this question I realized, I need to make the y value in the first while loop increase by 30% lintel it reaches 400 correct?

How do I make a loop around the two while loops to repeat over and over, without an infinity loop?

I appreciate any input or comments or ideas!

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;

public class MY_Proj04 extends JApplet
{
    int x, y;
    Color Background;
    public void init()
    {
        x = 100;
        y = 400;
        Background = getBackground();
    }

    public void paint(Graphics g)
    {
        // I tryed putting a while loop around the two following while loops and 
        // doing y = y * 30/100, I did this because the fill oval can't take a double
        // as one of its parameters.

        // 1st while loop
        while(y >= 0) // Ball goes up to (0,100)
        {
            g.setColor(Background);
            // fill the 500 by 500 square with it background color
            // any old draw will be covered
            g.fillRect(0, 0, 500, 500);
            g.setColor(Color.red);
            g.fillOval(x, y, 50, 50);
            for(long i = 1; i < 5000000; i++); //speed of ball
            y -=1;
        }

        // 2nd while loop
        while(y <= 400) // ball goes down to 400,100
        {
            g.setColor(Background);
            // fill the 500 by 500 square with it background color
            // any old draw will be covered
            g.fillRect(0, 0, 500, 500);
            g.setColor(Color.red);
            g.fillOval(x, y, 50, 50);
            for(long i = 1; i < 5000000; i++); //speed of ball
            y += 1;
        }
    }
}
Ryan Kempt
  • 4,200
  • 6
  • 30
  • 41
  • Take a look at [this example](http://stackoverflow.com/questions/19626338/japplet-creates-a-ball-that-bounces-and-gets-progressively-less-high-in-java/19626396#19626396) – MadProgrammer Sep 10 '14 at 23:53

1 Answers1

1

I tried this program in a different way. Its working. For the speed of ball, i used Thread.sleep() instead of the loop that you used. Also, a correction is that the first while loop in your code is moving ball back to the starting position. To make it bounce less than its previous height, you need to increase the RHS value (which is fixed in your code i.e. 0). One more thing, to give the ball look like its bouncing, I've done a few changes.

Here's the code:

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

/**<applet code="ball" height = "768" width = "1366" ></applet>*/

public class ball extends Applet implements Runnable
{
    int x,y,height,width,a;
    float z;
    public void start()
    {
         x=100; y=400; h=50; w=50; a=0;
         z = y;
         Thread th=new Thread(this);
         th.start();
    }

    public void run()
    {
        while(Math.round(z)!=0)
        {   
            while(y>=a) //Ball bounces till it reaches point a. Which in turn is increasing with each bounce till it is equal to 400.
            {
                y--;
                repaint();
                try{Thread.sleep(3);} // Speed of the ball.
                catch(Exception e){}
            }
            while(y<=400) // This loop gives ball a bouncing look each time it hits the floor (i.e. 400).
            {
                y++;
                repaint();
                if(y==400) 
                {
                    while(h!=40)
                    {   
                        y++;
                        height--;
                        x--;
                        width+=2;
                        repaint();
                        try{Thread.sleep(3);}
                        catch(Exception e){}
                    }   
                    while(h!=50)
                    {
                        y--;
                        height++;
                        x++;
                        widt--=2;
                        repaint();
                        try{Thread.sleep(3);}
                        catch(Exception e){}
                    }
                }
                try{Thread.sleep(3);}
                catch(Exception e){}
            }
            z=z*(0.7f); //This line and the next line calculates the value of a for the next bounce.
            a=Math.round(y-z); //round() function rounds the         floating value to nearest whole number and converts it into an integer.
        }
    }

    public void paint(Graphics g)
    {
        g.fillOval(x,y,width,height);
    }
}
Ulfalizer
  • 4,664
  • 1
  • 21
  • 30