Results 1 to 3 of 3
  1. #1
    Wangagat is offline Member
    Join Date
    Feb 2012
    Location
    Sweden
    Posts
    1
    Rep Power
    0

    Default How To Paint Multiple Objects Of Same Class Onto One JPanel

    I'm a university student and I'm having trouble with my assignment. Normally, I would just go to the lab time and ask the TA, but he's been sick all week so we haven't had any lab time and this assignment is due on Monday!

    the specific problem I have is related to creating a java application that displays a frame with a button allowing the user to create a ball that starts bouncing on the screen and rebounds off the frame boundaries.

    One of the exercises from a previous assignment was to create a similar program but that when run, would immediately display ONE ball bouncing. (which I got to work) Now, we have to modify our code and incorporate the button which allows us to create multiple balls.

    At first, I thought this would be an easy modification, but now I am getting confused about how to actually instantiated the Ball objects. My thought process is that I first have to make the ReboundPanel and button panel appear (which works), and then whenever the user presses the button, a new Ball object is instantiated and displayed onto the ReboundPanel. (which does not work at present)

    thanks to all for the help!

    Main program:

    Java Code:
    import java.awt.*;
    
    public class Rebound {
    
    public static void main(String[] args) {
    
        JFrame frame = new JFrame ("Rebound");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
    
        JPanel reboundPanel = new ReboundPanel();
        JPanel buttonPanel = new ButtonPanel();
        frame.getContentPane().add(reboundPanel);
        frame.getContentPane().add(buttonPanel);
        frame.pack();
        frame.setVisible(true);
    }
    }
    Panel where ball should appear:

    Java Code:
    import java.awt.*;
    
    public class ReboundPanel extends JPanel {
    
    private final int WIDTH = 400, HEIGHT = 300;
    
    public ReboundPanel() {
    
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
        setBackground (Color.black);
    
    }
    }
    Button Panel:

    Java Code:
    import java.awt.*;
    
    public class ButtonPanel extends JPanel {
    
    private final int WIDTH = 400, HEIGHT = 35;
    
    public ButtonPanel() {
    
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
        setBackground (Color.GRAY);
    
        JButton button = new JButton("New ball");
        add(button);
    
        button.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent event) {
    
                new Ball(); 
    
            }
        });
    }
    }
    Ball Class:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Ball extends JPanel {
    
    private final int DELAY = 20, IMAGE_SIZE = 35;
    
    private ImageIcon image;
    private Timer timer;
    private int x, y, moveX, moveY;
    
    
    public Ball() {
    
        timer = new Timer(DELAY, new ReboundListener());
        image = new ImageIcon ("/src/pa1/images/earth.gif");
        x = 0;
        y = 40;
        moveX = moveY = 3;
        draw(null);
        timer.start();
    }
    
    public void draw(Graphics page) {
    
        super.paintComponent (page);
        image.paintIcon (new ReboundPanel(), page, x, y);
    }
    
    private class ReboundListener implements ActionListener {
    
        public void actionPerformed (ActionEvent event) {
    
            x += moveX;
            y += moveY;
    
            if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
                moveX = moveX * -1;
    
            if (y <= 0 || x >= WIDTH-IMAGE_SIZE)
                moveY = moveY * -1;
    
            repaint();
        }
    }
    }

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

    Default Re: How To Paint Multiple Objects Of Same Class Onto One JPanel

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: How To Paint Multiple Objects Of Same Class Onto One JPanel

    First off, your Ball class is all messed up. I would suggest you make it a simple geometric entity that knows how to paint itself, and where. Extending JPanel, calling super.paintComponent(...) from a custom method and that too with a null Graphics reference are simply not kosher.

    The ReboundPanel (or a separate controller class) could have a java.util.List of Ball objects and iterate over the list in a paintComponent(...) override, painting each with its own Graphics reference.

    Start by incorporating whatever you understand of this advice, and post the fresh code for review.

    Oh, and this is a technical forum. We don't have the slightest interest in reading
    Normally, I would just go to the lab time and ask the TA, but he's been sick all week so we haven't had any lab time and this assignment is due on Monday!
    We've heard it all so many times that it no longer matters to us whether it might even be true in a few cases. And by stating your limited time frame, all you are exposing is your own poor time management.

    So stick with the technical details from now on.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Draw multiple class objects in one frame
    By jj007 in forum AWT / Swing
    Replies: 2
    Last Post: 01-14-2012, 06:32 PM
  2. Paint Graphics2D objects onto a JPanel
    By sublixt in forum Java 2D
    Replies: 10
    Last Post: 10-28-2011, 11:57 AM
  3. Multiple objects in a class help please
    By patcc2 in forum New To Java
    Replies: 12
    Last Post: 01-11-2011, 02:35 PM
  4. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  5. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 02:26 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
  •