Results 1 to 11 of 11
  1. #1
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Paint - Multiple Objects on JFrame

    Hello,
    I am trying to make a simple multi-bouncing ball program in java and I am having some trouble making it paint/run it properly...
    Here is my code:

    BetterMultiBall:
    Java Code:
    package Ball;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFrame;
    
    public class BetterMultiBall extends JFrame {
    
        Image dbi;
        Graphics dbg;
        List<BetterBall> b = new ArrayList<BetterBall>();
    
        public BetterMultiBall() {
            this.setVisible(true);
            this.setSize(400, 400);
            this.setResizable(false);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setBackground(Color.LIGHT_GRAY);
            b.add(new BetterBall(234, 100, Color.BLUE, 10, 2, 2));
            b.add(new BetterBall(150, 150, Color.RED, 15, 2, 1));
        }
    
        public void paint(Graphics g) {
            dbi = createImage(getWidth(), getHeight());
            dbg = dbi.getGraphics();
            paintComponent(dbg);
            g.drawImage(dbi, 0, 0, this);
        }
    
        public void paintComponent(Graphics g) {
            for (BetterBall ball : b) {
                ball.paintBall(g);
            }
            repaint();
        }
    
        public void move() {
            for (BetterBall ball : b) {
                ball.moveBall();
            }
    
        }
    
        public static void main(String[] args) {
            BetterMultiBall b = new BetterMultiBall();
        }
    }
    Better Ball class:
    Java Code:
    package Ball;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class BetterBall implements Runnable {
    
        int x, y, rad, xspeed, yspeed;
        Color c;
    
        public BetterBall(int x, int y, Color c, int rad, int xspeed, int yspeed) {
            this.x = x;
            this.y = y;
            this.c = c;
            this.rad = rad;
            this.xspeed = xspeed;
            this.yspeed = yspeed;
        }
    
        public void moveBall() {
            x += xspeed;
            y += yspeed;
    
            if (x < 0 || x > 400 - rad) {
                xspeed = -xspeed;
            }
    
            if (y < 0 || y > 400 - rad) {
                yspeed = -yspeed;
            }
    
        }
    
        public void paintBall(Graphics g) {
            g.setColor(c);
            g.fillOval(x, y, rad, rad);
        }
    
        @Override
        public void run() {
            try {
                while (true) {
                    moveBall();
                }
            } catch (Exception e) {
                System.out.println(e);
            }
    
        }
    }
    I am trying to paint 3 balls on the screen and make them move - I can see the ball on the screen when launched but its not moving and its giving me an error as:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Ball.BetterMultiBall.paintComponent(BetterMultiBall.java:40)
    	at Ball.BetterMultiBall.paint(BetterMultiBall.java:34)
    and the lines following are related to the RepaintManager.

    Any Help would be appreciated.
    Thanks.
    Last edited by galxion123; 05-01-2013 at 02:53 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Paint - Multiple Objects on JFrame

    You only initialise the array once you've already made the frame visible.
    So the first paint has an array of nulls to deal with.

    This really shouldn't be a JFrame.
    It should be a JPanel, and you should only be overriding paintComponent, not paint.

    I'm also not convinced this is the correct way for doing this sort of thing...but there are better animation people around here than I.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Paint - Multiple Objects on JFrame

    Quote Originally Posted by Tolls View Post
    You only initialise the array once you've already made the frame visible.
    So the first paint has an array of nulls to deal with.

    This really shouldn't be a JFrame.
    It should be a JPanel, and you should only be overriding paintComponent, not paint.

    I'm also not convinced this is the correct way for doing this sort of thing...but there are better animation people around here than I.
    Hmmm...But i have assigned values for indices 0,1,2 of Ballb does that still make it Null??..
    Also,JPanel gives me a lot of errors inside the constructor class..
    And I didn't quite understand the override part.. I use the paint for double buffer and the paintcomponent for drawing shapes/graphics..

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Paint - Multiple Objects on JFrame

    Java Code:
        public BetterMultiBall() {
            this.setVisible(true);
    That there starts off a chain of events that results in the repaint() for the JFrame being called, which will result in your paint() and paintComponent() methods being called.
    Except:
    Java Code:
            ballb[0] = new BetterBall(234, 100, Color.BLUE, 10, 2, 2);
            ballb[1] = new BetterBall(150, 150, Color.RED, 15, 2, 1);
            ballb[2] = new BetterBall(45, 184, Color.YELLOW, 20, 1, 1);
    
        }
    You don't initialise the array until here, afterwards.
    Hence the NPE.

    Your best bet (IMO) is to use a List of some sort, and simply loop over it in the paintComponent() of a JPanel.
    That way if it's called when there's no balls available it won't attempt to draw them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Paint - Multiple Objects on JFrame

    I tried using an ArrayList and I got through without any runtime errors but the ball still doesn't move
    (EDITED CODE IN OP)

    I just see the balls painted on the frame..


    Also,I have not really worked on this type of FOR loops...so how can I implement the same function in a traditional for loop..

    Java Code:
    for (BetterBall ball : b) {
                ball.paintBall(g);
            }
    Thanks.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Paint - Multiple Objects on JFrame

    No, that code is fine.
    Though you shouldn't have edited the original post as the first part of this thread makes no sense now.

    Look at your ball threads...there is nowhere in that code that tells the panel/frame/whatever that it needs to be redrawn.
    As far as your GUI is concerned it draws the balls once and that's that.
    All your Ball objects do is change some numbers (very rapidly).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Paint - Multiple Objects on JFrame

    Quote Originally Posted by Tolls View Post
    No, that code is fine.
    Though you shouldn't have edited the original post as the first part of this thread makes no sense now.

    Look at your ball threads...there is nowhere in that code that tells the panel/frame/whatever that it needs to be redrawn.
    As far as your GUI is concerned it draws the balls once and that's that.
    All your Ball objects do is change some numbers (very rapidly).
    Umm... I just removed the array objects and replaced it with ArrayList..and the move method change the x,y variables and then it is assigned to the paintBall though the main class i.e
    paintComponent...that does not work?..

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Paint - Multiple Objects on JFrame

    But look at the code.
    How does the GUI know that it needs to redraw itself?

    Ooh...just noticed you are calling repaint() in the paintComponent().
    I'm guessing you've locked up your EDT there.
    Don't call repaint() there.
    Don't override paint().
    Find a way, from your balls, of calling repaint on the frame (which should be a panel).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Paint - Multiple Objects on JFrame

    So,BetterBall should be a subclass of JPanel and I should call repaint() from there?..is that right?..

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Paint - Multiple Objects on JFrame

    Call repaint(), which means BetterBall needs to have a reference to the thing that does the drawing.

    The proper way would be to set up a listener mechanism, so the GUI can register itself as a listener on the balls, but I suspect that's a bit of a stretch for you at the moment.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Paint - Multiple Objects on JFrame

    Hey Tolls,
    I was messing around with the run() method after adding Runnable to the main class and guess what it worked! :)

    The code(working):

    BetterMultiBall:
    Java Code:
    package Ball;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFrame;
    
    public class BetterMultiBall extends JFrame implements Runnable {
    
        Image dbi;
        Graphics dbg;
        BetterBall ba;
        int curnum = 3;
        BetterBall ballb[];
        //List<BetterBall> b = new ArrayList<BetterBall>();
    
        public BetterMultiBall() {
            this.setVisible(true);
            this.setSize(400, 400);
            this.setResizable(false);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setBackground(Color.LIGHT_GRAY);
    //        b.add(new BetterBall(234, 100, Color.BLUE, 10, 2, 2));
    //        b.add(new BetterBall(150, 150, Color.RED, 15, 2, 1));
            ballb = new BetterBall[curnum];
    
            ballb[0] = new BetterBall(234, 100, Color.BLUE, 10, 2, 2);
            ballb[1] = new BetterBall(150, 150, Color.RED, 15, 2, 1);
            ballb[2] = new BetterBall(45, 184, Color.YELLOW, 20, 1, 1);
    
        }
    
        public void paint(Graphics g) {
            dbi = createImage(getWidth(), getHeight());
            dbg = dbi.getGraphics();
            paintComponent(dbg);
            g.drawImage(dbi, 0, 0, this);
            repaint();
        }
    
        public void paintComponent(Graphics g) {
    
            for (int i = 0; i <= curnum-1; i++) {
                ballb[i].paintBall(g);           
            }
    //if array list is used ->
    //        for (BetterBall ball : b) {
    //            ball.paintBall(g);
    //        }
            repaint();
        }
    
        public void move() {
    //if array list is used ->
    //        for (BetterBall ball : b) {
    //            ball.moveBall();
    //        }
            for (int i = 0; i <= curnum-1; i++) {
                ballb[i].moveBall();
                
            }
        }
    
        public static void main(String[] args) {
            BetterMultiBall bal = new BetterMultiBall();
            Thread d = new Thread(bal);
            d.start();
        }
    
        @Override
        public void run() {
            try {
    
                while (true) {
                    move();
                    Thread.sleep(5);
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    BetterBall:
    Java Code:
    package Ball;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class BetterBall implements Runnable {
    
        int x, y, rad, xspeed, yspeed;
        Color c;
    
        public BetterBall(int x, int y, Color c, int rad, int xspeed, int yspeed) {
            this.x = x;
            this.y = y;
            this.c = c;
            this.rad = rad;
            this.xspeed = xspeed;
            this.yspeed = yspeed;
        }
    
        public void moveBall() {
            x += xspeed;
            y += yspeed;
            if (x < 0 || x > 400 - rad) {
                xspeed = -xspeed;
            }
    
            if (y < 0 || y > 400 - rad) {
                yspeed = -yspeed;
            }
        
    
        }
    
        public void paintBall(Graphics g) {
            g.setColor(c);
            g.fillOval(x, y, rad, rad);
    
        }
    
        @Override
        public void run() {
            try {
                while (true) {
                    moveBall();
    
                }
            } catch (Exception e) {
                System.out.println(e);
            }
    
        }
    }
    I just had to call the move() method of the main class from inside of the while(true) of the run method!
    Also,both the arraylist and the object method of storing different BetterBall class object worked!
    Thanks for the help Tolls.

Similar Threads

  1. JFrame cannot paint?
    By pandaman0212 in forum AWT / Swing
    Replies: 7
    Last Post: 05-04-2013, 07:19 AM
  2. How to paint multiple instances of class?
    By Zigster in forum AWT / Swing
    Replies: 5
    Last Post: 05-29-2012, 04:32 PM
  3. Replies: 2
    Last Post: 02-17-2012, 01:49 PM
  4. JFrame and paint()
    By ninjaturtlez in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 03:33 AM
  5. Paint Graphics2D objects onto a JPanel
    By sublixt in forum Java 2D
    Replies: 10
    Last Post: 10-28-2011, 11:57 AM

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
  •