Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Bouncing ball

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default 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)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    i dont think the classes recognize one another

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bouncing ball

    There are some missing import statements in the Color class. Look at what classes the compiler can not find definitions for, look in the API doc and find what package those classes are in and add an import statement for that package.
    See the other classes for what import statements they use.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    ATB (April 6th, 2014)

  8. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Bouncing ball

    thanks! totally works!!

Similar Threads

  1. Help with simple "bouncing" ball Swing application
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 7th, 2013, 02:07 AM
  2. Help with bouncing on a panel
    By hellhunt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2012, 08:28 PM
  3. Help with bouncing ball
    By aortell24 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: August 1st, 2012, 03:20 PM
  4. Bouncing Balls
    By roza in forum Threads
    Replies: 6
    Last Post: June 7th, 2011, 10:10 AM
  5. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM

Tags for this Thread