# Bouncing ball

• April 6th, 2014, 03:39 PM
ATB
Bouncing ball
so im a new programmer and im bsing my way through my first online course. im not asking for answers but please take that into consideration when explaining please.

im supposed to make a bouncing ball and the professor has given me 3 classes worth of code.

circle class
Code :

```public class Circle { private int x, y, diameter; private Color color;   public Circle( ) { x = 0; y = 0; diameter = 10; color = Color.BLACK; }   public Circle( int sX, int sY, int sDiameter, Color sColor ) { x = sX; y = sY; diameter = sDiameter; color = sColor; }   public int getX( ) { return x; }   public int getY( ) { return y; }   public int getDiameter( ) { return diameter; }   public Color getColor( ) { return color; }   public void setX( int newX ) { x = newX; }   public void setY( int newY ) { y = newY; }   public void setDiameter( int newDiameter ) { diameter = newDiameter; }   public void setColor( Color newColor ) { color = newColor; }   public void draw( Graphics g ) { g.setColor( color ); g.fillOval( x, y, diameter, diameter ); } public void draw(Graphics g,int newX,int newY,int d1, int d2) { g.setColor( color ); g.fillOval( newX, newY, d1, d2); } }```

pause code
Code :

```public class Pause { /** wait method * @param seconds number of seconds to pause */ public static void wait( double seconds ) { try { Thread.sleep( (int)( seconds * 1000 ) ); } catch ( InterruptedException e ) { e.printStackTrace( ); } } }```

and bouncingball class
Code :

```import java.awt.Color; import java.awt.Graphics;   import javax.swing.JApplet;   public class BouncingBall extends JApplet { public void paint(Graphics g) { super.paint(g);   int x=50,y=0,diameter=20; Color ballColor=Color.BLUE; double t=.04; boolean ballUp=true; int bounce=0; Circle ball = new Circle(x,y,diameter,ballColor); while (bounce<12) { ball.draw(g); g.drawLine(0, 180, 300, 180); Pause.wait(t); g.clearRect( 0, 0, 200, 200);   if (ballUp) ball.setY( ball.getY( )+10); else ball.setY( ball.getY( )-10); if (ball.getY()==160) { ballUp=false; bounce=bounce+1; y=y+10; Pause.wait(t); ball.draw(g,45,170,30,10); g.drawLine(0, 180, 300, 180); Pause.wait(t+0.05); g.clearRect( 0, 0, 200, 200);   } if (ball.getY()==y) ballUp=true; } g.clearRect( 0, 0, 500, 500); ball.draw(g); g.drawLine(0, 180, 300, 180); }     }```

how should i go about modifing the code to get the bouncing ball?

these are the instructor's specific instructions:
assume each time the
ball hits the ground, its shape changes from circle to
an oval. Modify classes provided in course slides so
we can see a slow movement of the ball along with
the changes occurring when the ball hits the
ground
• April 6th, 2014, 04:00 PM
Norm
Re: Bouncing ball
The Circle class has lost the indentation of the nested statements.

Is the "ground" the side at maximum y value? The bounces on the other three sides are normal.
• April 6th, 2014, 04:05 PM
ATB
Re: Bouncing ball
this may seem like a stupid question but r they all supposed to be in one class or three different ones in one workspace?
• April 6th, 2014, 04:12 PM
Norm
Re: Bouncing ball
The three classes are ok.

Have you executed the code? What is supposed to be changed? The ball bounces and deforms when I execute it.
• April 6th, 2014, 04:38 PM
ATB
Re: Bouncing ball
it gives me a ton of error messages:
Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problems:
The constructor Circle(int, int, int, Color) is undefined
The method draw(Graphics) from the type Circle refers to the missing type Graphics
The method draw(Graphics, int, int, int, int) from the type Circle refers to the missing type Graphics
The method draw(Graphics) from the type Circle refers to the missing type Graphics

at assignment9.BouncingBall.paint(BouncingBall.java:1 8)
at javax.swing.RepaintManager\$3.run(Unknown Source)
at javax.swing.RepaintManager\$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain\$1.doIntersectionPri vilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
at javax.swing.RepaintManager.access\$1100(Unknown Source)
at javax.swing.RepaintManager\$ProcessingRunnable.run( Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access\$200(Unknown Source)
at java.awt.EventQueue\$3.run(Unknown Source)
at java.awt.EventQueue\$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain\$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)