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