944,134 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 10123
  • Java RSS
Feb 2nd, 2005
0

Making a ball move down and up in an Applet

Expand Post »
I took a course in C and now im into java. I never created anywith graphics in C. I need to finish a program in C that makes a red ball appear on a blue background and then move down and up the screen.

Any information of how to at least draw an oval or make it move would help...

I know that I need to make the call appear in the center then paint over it and make it appear slightly lower and loop the process, but I do not know any of the code...

This is what I have so far:

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.applet.Applet;
  3.  
  4. public class BouncingBall extends Applet {
  5.  
  6. //Dimensions of Applet:
  7. public final int width = 500;
  8. public final int height = 500;
  9. //Size and speed of ball:
  10. public final int diameter = 20;
  11. public final int speed = 5;
  12. //Milliseconds to pause between frames:
  13. public final int pause = 20;
  14.  
  15. public void paint (Graphics page) {
  16. setBackground(Color.blue);
  17.  
  18.  
  19. //Loop forever;
  20. while (true) {
  21.  
  22. //Pause between frames:
  23. try { Thread.sleep(pause) ; } catch (Exception e) { };
  24. }
  25. }
  26. }


Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mooonky is offline Offline
8 posts
since Dec 2004
Feb 2nd, 2005
0

Re: Making a ball move down and up in an Applet

You'd have to use repaint() I think, and keep re-drawing the oval just 1 pixel lower. I think theres a method called drawOval or something but dont remember what class it's in
Reputation Points: 10
Solved Threads: 0
Light Poster
Gink is offline Offline
46 posts
since Feb 2005
Feb 2nd, 2005
0

Re: Making a ball move down and up in an Applet

Java Syntax (Toggle Plain Text)
  1. drawOval(int x, int y, int width, int height)

Just use threads to increment the x and y value position at certain intervals.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Feb 2nd, 2005
0

Re: Making a ball move down and up in an Applet

I found the method
in java.awt.Graphics

drawOval(int x, int y, int width, int height)
Draws the outline of an oval.

while(true){
drawOval(50,y,50,50);
repaint();
y++;
}
if you want it to bounce, figure out the size of the screen you are on, find the lowest pixel, and if y = that value start using y-- instead. 2 if statements would work for it
Reputation Points: 10
Solved Threads: 0
Light Poster
Gink is offline Offline
46 posts
since Feb 2005
Feb 3rd, 2005
0

Re: Making a ball move down and up in an Applet

Thanks, but my IDE NetBeans is giving me an error regarding drawOval here is the code I have now...I forgot to put that it has to start in the center of the page of 500 by 500 pixels and it should bounce back up and then bounce back down forever...I have been trying to get this right all day...

Java Syntax (Toggle Plain Text)
  1. //This applet draws an animated bouncing ball with uniform speed.
  2. //The ball starts at the center of the graphics page and bounces off
  3. //the top and bottom
  4.  
  5. import java.awt.*;
  6. import java.applet.Applet;
  7.  
  8. public class BouncingBall extends Applet {
  9.  
  10. //Dimensions of Applet:
  11. public final int width = 500;
  12. public final int height = 500;
  13. //Size and speed of ball:
  14. public final int diameter = 20;
  15. public final int speed = 5;
  16. //Milliseconds to pause between frames:
  17. public final int pause = 20;
  18.  
  19. public void paint (Graphics page) {
  20. setBackground(Color.blue);
  21. page.drawOval(250, int y, 20, 20);
  22. page.setColor(Color.red);
  23. //Loop forever;
  24. while (true) {
  25. drawOval(50,y,50,50);
  26. repaint();
  27. y++;
  28. //Pause between frames:
  29. try { Thread.sleep(pause) ; } catch (Exception e) { };
  30. }
  31. }
  32. }
  33.  
  34. //End-of-File

Thanks again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mooonky is offline Offline
8 posts
since Dec 2004
Feb 9th, 2005
0

Re: Making a ball move down and up in an Applet

Hi, if your getting an error with drawOval then I would try drawRect();
In my opinion the while(true) and repaint(); should be in the run(); method since you should create a Thread and implement the Runnable interface.
When you call repaint in Java it automatically calls another method called update(); You should implement your own update method in your code like

public void update()
{
paint(page);
}

To reduce flicker. I hope this helps you some.
Reputation Points: 17
Solved Threads: 8
Junior Poster in Training
Banderson is offline Offline
66 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Applet Image Displaying
Next Thread in Java Forum Timeline: Key Events





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC