Making a ball move down and up in an Applet

Reply

Join Date: Dec 2004
Posts: 8
Reputation: Mooonky is an unknown quantity at this point 
Solved Threads: 0
Mooonky Mooonky is offline Offline
Newbie Poster

Making a ball move down and up in an Applet

 
0
  #1
Feb 2nd, 2005
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

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

 
0
  #2
Feb 2nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

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

 
0
  #3
Feb 2nd, 2005
  1. drawOval(int x, int y, int width, int height)

Just use threads to increment the x and y value position at certain intervals.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 46
Reputation: Gink is an unknown quantity at this point 
Solved Threads: 0
Gink Gink is offline Offline
Light Poster

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

 
0
  #4
Feb 2nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 8
Reputation: Mooonky is an unknown quantity at this point 
Solved Threads: 0
Mooonky Mooonky is offline Offline
Newbie Poster

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

 
0
  #5
Feb 3rd, 2005
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...

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 47
Reputation: Banderson is an unknown quantity at this point 
Solved Threads: 4
Banderson's Avatar
Banderson Banderson is offline Offline
Light Poster

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

 
0
  #6
Feb 9th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC