•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,273 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,320 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 3370 | Replies: 5
![]() |
•
•
Join Date: Dec 2004
Posts: 8
Reputation:
Rep Power: 0
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:
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
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
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:
Rep Power: 4
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:
Rep Power: 0
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
//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-FileThanks again
•
•
Join Date: Aug 2004
Location: North Carolina
Posts: 27
Reputation:
Rep Power: 5
Solved Threads: 2
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Homework Help: How can I get this ball to Move? (Java)
- modifying all of the instances in an array at once (Java)
Other Threads in the Java Forum
- Previous Thread: Applet Image Displaying
- Next Thread: Key Events



Linear Mode