| | |
Making a ball move down and up in an Applet
![]() |
•
•
Join Date: Dec 2004
Posts: 8
Reputation:
Solved Threads: 0
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:
Thanks
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)
import java.awt.*; import java.applet.Applet; public class BouncingBall extends Applet { //Dimensions of Applet: public final int width = 500; public final int height = 500; //Size and speed of ball: public final int diameter = 20; public final int speed = 5; //Milliseconds to pause between frames: public final int pause = 20; public void paint (Graphics page) { setBackground(Color.blue); //Loop forever; while (true) { //Pause between frames: try { Thread.sleep(pause) ; } catch (Exception e) { }; } } }
Thanks
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Java Syntax (Toggle Plain Text)
drawOval(int x, int y, int width, int height)
Just use threads to increment the x and y value position at certain intervals.
•
•
Join Date: Feb 2005
Posts: 46
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2004
Posts: 8
Reputation:
Solved Threads: 0
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...
Thanks again
Java Syntax (Toggle Plain Text)
//This applet draws an animated bouncing ball with uniform speed. //The ball starts at the center of the graphics page and bounces off //the top and bottom import java.awt.*; import java.applet.Applet; public class BouncingBall extends Applet { //Dimensions of Applet: public final int width = 500; public final int height = 500; //Size and speed of ball: public final int diameter = 20; public final int speed = 5; //Milliseconds to pause between frames: public final int pause = 20; public void paint (Graphics page) { setBackground(Color.blue); page.drawOval(250, int y, 20, 20); page.setColor(Color.red); //Loop forever; while (true) { drawOval(50,y,50,50); repaint(); y++; //Pause between frames: try { Thread.sleep(pause) ; } catch (Exception e) { }; } } } //End-of-File
Thanks again
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.
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.
![]() |
Similar Threads
- Breakout Ball bouncing physics (Game Development)
- Homework Help: How can I get this ball to Move? (Java)
Other Threads in the Java Forum
- Previous Thread: Applet Image Displaying
- Next Thread: Key Events
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing text-file thread threads tree windows






