Results 1 to 6 of 6
  1. #1
    vettera is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    vettera is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Cant create more than one ball

    Quote Originally Posted by Fubarable View Post
    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.
    Well i guess you are wrong. I just signed up and I'm still getting use to everything so I'm sorry about that. But I did read each thread I'm using a JPanel like you said for me to.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Cant create more than one ball

    Quote Originally Posted by vettera View Post
    Well i guess you are wrong. ... But I did read each thread ...
    But still missed the part about using code tags?

    I guess he was right.

    db

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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);
       }
    }

  6. #6
    vettera is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Cant create more than one ball

    Quote Originally Posted by Fubarable View Post
    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);
       }
    }
    Thanks for your help I read your thread and I will get working on the program agian

Similar Threads

  1. Ball program
    By codeStone in forum Advanced Java
    Replies: 10
    Last Post: 02-25-2011, 01:32 PM
  2. Need help with a third ball in game.
    By vlan in forum Java Applets
    Replies: 2
    Last Post: 05-30-2010, 03:37 PM
  3. Box Ball Java
    By fcgb9115 in forum New To Java
    Replies: 1
    Last Post: 04-19-2010, 02:33 AM
  4. How to create a bouncing ball animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:43 PM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •