Results 1 to 6 of 6
Thread: Cant create more than one ball
- 09-17-2011, 06:56 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Cant create more than one ball
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class ball extends JPanel implements Runnable {
final int Delay = 5;
int y = 0;
int x = 0;
int r = 0;
int gr = 0;
int b = 0;
public ball(int red,int green,int blue){
r = red;
gr = green;
b = blue;
}
public void addNotify(){
super.addNotify();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(new Color(r,gr,b));
g.fillOval(x, y, 10, 10);
}
Random rand = new Random();
int num = rand.nextInt(500);
int num2 = rand.nextInt(500);
public void cycle(){
y++;
x++;
if(num == y){
num = rand.nextInt(500);
y = 0;
}
if(num == x){
num2 = rand.nextInt(500);
x = 0;
}
if(x > 500 || y > 500){
x = 0;
y = 0;
}
}
public void run(){
while(true){
long starttime , total , sleep;
cycle();
repaint();
starttime = System.currentTimeMillis();
total = System.currentTimeMillis() - starttime;
sleep = Delay - total;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class main {
public static void main(String[] args){
JFrame j = new JFrame();
j.setTitle("Moving Ball");
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(500,500);
ball b = new ball(70,0,130);
Thread circle = new Thread(b);
j.add(b);
circle.start();
ball b1 = new ball(0,100,0);
Thread circle2 = new Thread(b1);
j.add(b1);
circle2.start();
j.setVisible(true);
}
}
-
Re: Cant create more than one ball
You never replied to help given in your last thread, so I can only assume that you didn't read our posts, and that you'll do the same here in this thread. I hope I'm wrong.
- 09-17-2011, 08:05 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
- 09-17-2011, 02:02 PM #4
-
Re: Cant create more than one ball
If your goal is to show more than one ball in the same 500 x 500 region, then one possible problem I see is that your current code as written ties one ball image to one JPanel, and while you can display overlapped JPanels on the JFrame, you really don't want to mess with this.
Suggestions:
- Separate the concept of Ball from JPanel. In other words, create a Ball object that doesn't extend JPanel. Then a single JPanel can hold more than one Ball.
- Ball already has x and y fields, and so keep them.
- Ball already has a public method cycle() that will move it, so keep it.
- Give Ball a paint(Graphics g) method so that it can draw itself.
- The JPanel will tell each Ball object that it holds to paint itself within the JPanel's paintComponent method.
- Use a Swing Timer instead of while (true) as your game loop.
- You can have your JPanel hold one or two Ball objects, or if you want to really get fancy, an array list of Ball objects -- ArrayList<Ball>. If the latter, you'll loop through the array list in your Timer method calling cycle() on each Ball in the list, then when done with this call repaint() on the JPanel.
- Then in Ball's paintComponent again loop through the array list calling paint(g) on each Ball in the list.
- I'm not sure what your sleep time calculation is trying to do as you're getting two system times in a row and getting the difference -- why? Why not just use a constant time as the Timer's delay.
- And again, please reply to this reply even if it's to complain. Again, we're all volunteers and like to know that our time spent considering your problem has not been wasted, that you at least have taken the time to read and consider the recommendations. If you don't understand anything here or disagree, please let us know.
- And please read up and learn to use code tags when posting in this forum as they make your code much easier to read. For example, see how your code is much easier to read below when I use tags:
Java Code:import java.awt.*; import java.util.*; import javax.swing.*; public class Ball extends JPanel implements Runnable { final int Delay = 5; int y = 0; int x = 0; int r = 0; int gr = 0; int b = 0; public Ball(int red, int green, int blue) { r = red; gr = green; b = blue; } public void addNotify() { super.addNotify(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(new Color(r, gr, b)); g.fillOval(x, y, 10, 10); } Random rand = new Random(); int num = rand.nextInt(500); int num2 = rand.nextInt(500); public void cycle() { y++; x++; if (num == y) { num = rand.nextInt(500); y = 0; } if (num == x) { num2 = rand.nextInt(500); x = 0; } if (x > 500 || y > 500) { x = 0; y = 0; } } public void run() { while (true) { long starttime, total, sleep; cycle(); repaint(); starttime = System.currentTimeMillis(); total = System.currentTimeMillis() - starttime; sleep = Delay - total; try { Thread.sleep(sleep); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { JFrame j = new JFrame(); j.setTitle("Moving Ball"); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j.setSize(500, 500); Ball b = new Ball(70, 0, 130); Thread circle = new Thread(b); j.add(b); circle.start(); Ball b1 = new Ball(0, 100, 0); Thread circle2 = new Thread(b1); j.add(b1); circle2.start(); j.setVisible(true); } }
- 09-17-2011, 05:26 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Ball program
By codeStone in forum Advanced JavaReplies: 10Last Post: 02-25-2011, 01:32 PM -
Need help with a third ball in game.
By vlan in forum Java AppletsReplies: 2Last Post: 05-30-2010, 03:37 PM -
Box Ball Java
By fcgb9115 in forum New To JavaReplies: 1Last Post: 04-19-2010, 02:33 AM -
How to create a bouncing ball animation
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:43 PM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks