Results 1 to 6 of 6
  1. #1
    ShaolinMunky is offline Computer Science Student
    Join Date
    Feb 2012
    Location
    Savannah, GA
    Posts
    4
    Rep Power
    0

    Question More bouncing balls...

    Hey all!

    I need some help with graphics! Here's what I'm trying to do:

    I need to create an applet that generates bouncing balls. The number of balls are dependent on buttons used to add and remove them. They all enter the panel from the origin.

    Here's what my problem is:

    When I click the "+1" button, the ball increases in length and increases in speed -- as if the increment is a multiplier all around. And the "-1" button does the opposite -- however it won't remove the ball at all.

    I am utilizing an ArrayList, but really don't understand how to incorporate graphics so I have multiple images -- I just know that this is the data structure I need to use. So, how can I adjust my program to create multiple (unlimited) balls at the click of a button? My code is below.

    Thanks!
    - SM

    Java Code:
    
    import java.util.ArrayList;
    import java.util.List;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    
    public class Exercise18_19 extends JApplet {
    
      private static List<Ball> ballList = new ArrayList<Ball>();   
        
      public Exercise18_19() {
        add(new BallControl());
      }
    
      public static void main(String[] args) {
        Exercise18_19 applet = new Exercise18_19();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("BounceBallApp");
        frame.add(applet, BorderLayout.CENTER);
        frame.setSize(400, 320);
        frame.setVisible(true);
      }
        
    
    
    class BallControl extends JPanel{
      private Ball ball = new Ball();
      private JButton jbtSuspend = new JButton("Suspend");
      private JButton jbtResume = new JButton("Resume");
      private JButton jbtPlusOne = new JButton("+1");
      private JButton jbtMinusOne = new JButton("-1");
      private JScrollBar jsbDelay = new JScrollBar();
    
      public BallControl() {
        // Group buttons in a panel
        JPanel panel = new JPanel();
        panel.add(jbtSuspend);
        panel.add(jbtResume);
        panel.add(jbtPlusOne);
        panel.add(jbtMinusOne);
    
        // Add ball and buttons to the panel
        ball.setBorder(new javax.swing.border.LineBorder(Color.red));
        jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
        ball.setDelay(jsbDelay.getMaximum());
        setLayout(new BorderLayout());
        add(jsbDelay, BorderLayout.NORTH);
        add(ball, BorderLayout.CENTER);
        add(panel, BorderLayout.SOUTH);
    
        // Register listeners
        jbtSuspend.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            ball.suspend();
          }
        });
        jbtResume.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            ball.resume();
          }
        });
        jsbDelay.addAdjustmentListener(new AdjustmentListener() {
          public void adjustmentValueChanged(AdjustmentEvent e) {
            ball.setDelay(jsbDelay.getMaximum() - e.getValue());
          }
        });
        jbtPlusOne.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            ball.increment();
          }
        });
        jbtMinusOne.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            ball.decrement();
          }
        });
      }
    }
    
    
    //***************************************************************
    
    
    
    public class Ball extends JPanel {
      private int delay = 1000;  
      // Create a timer with delay 1000 ms
      private Timer timer = new Timer(delay, new TimerListener());
    
      private int x = 0; private int y = 0; // Current ball position
      private int radius = 5; // Ball radius
      private int dx = 2; // Increment on ball's x-coordinate
      private int dy = 2; // Increment on ball's y-coordinate
    
      public Ball() {
       timer.start();
       //radius = this.radius;
       //x = this.x;
       //y = this.y;
       //dx = this.dx;
       //dy = this.dy;
      }
      
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Ball ball : ballList){
              // Check boundaries
          if (x < radius) dx = Math.abs(dx);
          if (x > getWidth() - radius) dx = -Math.abs(dx);
          if (y < radius) dy = Math.abs(dy);
          if (y > getHeight() - radius) dy = -Math.abs(dy);
    
          // Adjust ball position
          x += dx;
          y += dy;
          g.setColor(Color.red);
          g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
        }//close for loop
      }//close paint method
      
      
      private class TimerListener implements ActionListener {
        /** Handle the action event */
        public void actionPerformed(ActionEvent e) {
           for (Ball ball : ballList){
            repaint();
           }
        }
      }//close timer class
    
      public void suspend() {
        timer.stop(); // Suspend timer
      }
    
      public void resume() {
        timer.start(); // Resume timer
      }
    
      public void setDelay(int delay) {
        this.delay = delay;
        timer.setDelay(delay); //Adjusts delay per slider
      }
      
      public void increment(){
        ballList.add(new Ball()); 
        System.out.println("plus one");
      }
      
      public void decrement(){
        int last = ballList.size();
        if(last != 0)
        ballList.remove(last-1);
        System.out.println("minus one");
      }
      
     }//close ball class 
    }//close class -- end of file

  2. #2
    ShaolinMunky is offline Computer Science Student
    Join Date
    Feb 2012
    Location
    Savannah, GA
    Posts
    4
    Rep Power
    0

    Default Re: More bouncing balls...

    Anyone?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: More bouncing balls...

    how can I adjust my program to create multiple (unlimited) balls at the click of a button?
    Use the paintComponent method in the BallControl call to call the balls for them to display themselves. Have a list of balls that it calls passing each the Graphics object.

    When you click a button a new button is created and added to the list.

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

    Default Re: More bouncing balls...

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

  5. #5
    kaplis is offline Member
    Join Date
    May 2012
    Posts
    9
    Rep Power
    0

    Default Re: More bouncing balls...

    Here is a corrected version with random colors for each ball and avoiding the use of List<> sintaxis that is not yet covered in chapter 18 of Liang´s Java programming book 8th edition from where this exercise is extracted, so unsafe check alert would appear when compiling it.

    Java Code:
    import java.util.ArrayList;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Exercise18_19 extends JApplet
    {
        private static ArrayList ballList = new ArrayList();
        private JButton jbtPlusOne = new JButton("+1");
        private JButton jbtMinusOne = new JButton("-1");
    
        public Exercise18_19()
        {
            add(new BallControl());
        }
    
        public static void main(String[] args)
        {
            Exercise18_19 applet = new Exercise18_19();
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Exercise18_19");
            frame.add(applet, BorderLayout.CENTER);
            frame.setSize(400, 300);
            frame.setVisible(true);
        }
    
        class BallControl extends JPanel
        {
            private Ball ball = new Ball();
            private JButton jbtSuspend = new JButton("Suspend");
            private JButton jbtResume = new JButton("Resume");
            private JScrollBar jsbDelay = new JScrollBar();
    
            public BallControl()
            {
                // Group buttons in a panel
                JPanel panel = new JPanel();
                panel.add(jbtSuspend);
                panel.add(jbtResume);
                panel.add(jbtPlusOne);
                panel.add(jbtMinusOne);
    
                // Add ball and buttons to the panel
                ball.setBorder(new javax.swing.border.LineBorder(Color.red));
                jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
                ball.setDelay(jsbDelay.getMaximum());
                setLayout(new BorderLayout());
                add(jsbDelay, BorderLayout.NORTH);
                add(ball, BorderLayout.CENTER);
                add(panel, BorderLayout.SOUTH);
    
                // Register listeners
                jbtSuspend.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        ball.suspend();
                    }
                });
                jbtResume.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        ball.resume();
                    }
                });
                jsbDelay.addAdjustmentListener(new AdjustmentListener()
                {
                    public void adjustmentValueChanged(AdjustmentEvent e)
                    {
                        ball.setDelay(jsbDelay.getMaximum() - e.getValue());
                    }
                });
                jbtPlusOne.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        ball.increment();
                    }
                });
                jbtMinusOne.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        ball.decrement();
                    }
                });
            }
        }
    
        public class Ball extends JPanel
        {
            private int delay = 1000;
            // Create a timer with delay 1000 ms
            private Timer timer = new Timer(delay, new TimerListener());
    
            private int x = 0;
            private int y = 0; // Current ball position
            private int radius = 5; // Ball radius
            private int dx = 2; // Increment on ball's x-coordinate
            private int dy = 2; // Increment on ball's y-coordinate
    
            private Color[] colorPalette = new Color[]{Color.red, Color.blue, Color.cyan, 
                    Color.green, Color.gray, Color.yellow, Color.black, Color.magenta,
                    Color.darkGray, Color.LIGHT_GRAY, Color.orange, Color.pink, Color.white};
    
            private Color color;
    
            public Ball()
            {
                timer.start();
            }
    
            public void setColor(Color color)
            {
                this.color = color;
            }
    
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);
    
                for (Object ball : ballList)  // if you use List<Ball> then change
                                                   // to: for(Ball ball : ballList)
                {
                    // Check boundaries
                    if (((Ball) ball).x < radius) ((Ball) ball).dx = // If you use List<Ball>
                                 Math.abs(((Ball) ball).dx);          // instead of ArrayList
                                                                             // change these lines                            
                                                                             // to the following:
                                                                             // if(ball.x < radius)
                                                                             // ball.dx = Math.abs
                                                                             // (ball.dx); 
                                                                             // etc., etc...
                    
                    if (((Ball) ball).x > getWidth() - radius) ((Ball) ball).dx =
                            -Math.abs(((Ball) ball).dx);   // Eliminating Math.abs 
                                                                   // from this line has
                                                                   // the same result
                  
                    if (((Ball) ball).y < radius) ((Ball) ball).dy =
                             Math.abs(((Ball) ball).dy);
                  
                    if (((Ball) ball).y > getHeight() - radius) ((Ball) ball).dy =
                            -Math.abs(((Ball) ball).dy);  // Eliminating Math.abs 
                                                                  // from this line
                                                                  // has the same result
    
                    // Adjust ball position
                    ((Ball) ball).x += ((Ball) ball).dx;
                    ((Ball) ball).y += ((Ball) ball).dy;
                    g.setColor(((Ball) ball).color);
                    g.fillOval(((Ball) ball).x - radius,
                            ((Ball) ball).y - radius, radius * 2, radius * 2);
                } //close for loop
            } //close paint method
    
    
            private class TimerListener implements ActionListener
            {
                /**
                 * Handle the action event
                 */
                public void actionPerformed(ActionEvent e)
                {
                    repaint();
                }
            }//close timer class
    
            public void suspend()
            {
                timer.stop(); // Suspend timer
                jbtPlusOne.setEnabled(false);
                jbtMinusOne.setEnabled(false);
            }
    
            public void resume()
            {
                timer.start(); // Resume timer
                jbtPlusOne.setEnabled(true);
                jbtMinusOne.setEnabled(true);
            }
    
            public void setDelay(int delay)
            {
                this.delay = delay;
                timer.setDelay(delay); //Adjusts delay per slider
            }
    
            public void increment()
            {
                int colorPaletteIndex = (int) (Math.random() * colorPalette.length);
                Ball tempBall = new Ball();
                tempBall.setColor(colorPalette[colorPaletteIndex]);
                ballList.add(tempBall);
            }
    
            public void decrement()
            {
                int lastBall = ballList.size();
                if (lastBall >= 1)
                    ballList.remove(lastBall - 1);
            }
        } //close ball class
    } //close class -- end of file
    Last edited by kaplis; 05-05-2012 at 06:47 AM.

  6. #6
    kaplis is offline Member
    Join Date
    May 2012
    Posts
    9
    Rep Power
    0

    Default Re: More bouncing balls...

    A definitive version:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class Exercise18_19 extends JApplet {
    
        private static List<Ball> ballList = new ArrayList<Ball>();
    
        public Exercise18_19() {
            add(new BallControl());
        }
    
        public static void main(String[] args) {
            Exercise18_19 applet = new Exercise18_19();
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Exercise18_19");
            frame.add(applet, BorderLayout.CENTER);
            frame.setSize(400, 320);
            frame.setVisible(true);
        }
    
    
        class BallControl extends JPanel {
            private Ball ball = new Ball();
            private JButton jbtSuspend = new JButton("Suspend");
            private JButton jbtResume = new JButton("Resume");
            private JButton jbtPlusOne = new JButton("+1");
            private JButton jbtMinusOne = new JButton("-1");
            private JScrollBar jsbDelay = new JScrollBar();
    
            public BallControl() {
                // Group buttons in a panel
                JPanel panel = new JPanel();
                panel.add(jbtSuspend);
                panel.add(jbtResume);
                panel.add(jbtPlusOne);
                panel.add(jbtMinusOne);
    
                // Add ball and buttons to the panel
                ball.setBorder(new javax.swing.border.LineBorder(Color.red));
                jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
                ball.setDelay(jsbDelay.getMaximum());
                setLayout(new BorderLayout());
                add(jsbDelay, BorderLayout.NORTH);
                add(ball, BorderLayout.CENTER);
                add(panel, BorderLayout.SOUTH);
    
                // Register listeners
                jbtSuspend.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ball.suspend();
                    }
                });
                jbtResume.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ball.resume();
                    }
                });
                jsbDelay.addAdjustmentListener(new AdjustmentListener() {
                    public void adjustmentValueChanged(AdjustmentEvent e) {
                        ball.setDelay(jsbDelay.getMaximum() - e.getValue());
                    }
                });
                jbtPlusOne.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ball.increment();
                    }
                });
                jbtMinusOne.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ball.decrement();
                    }
                });
            }
        }
    
    
    //***************************************************************
    
    
        public class Ball extends JPanel {
            private int delay = 1000;
            // Create a timer with delay 1000 ms
            private Timer timer = new Timer(delay, new TimerListener());
    
            private int x = 0;
            private int y = 0; // Current ball position
            private int radius = 5; // Ball radius
            private int dx = 2; // Increment on ball's x-coordinate
            private int dy = 2; // Increment on ball's y-coordinate
    
            private Color[] colorPalette = new Color[]{Color.red, Color.blue, Color.cyan, Color.green, Color.gray, Color.yellow, Color.black, Color.magenta,
                    Color.darkGray, Color.LIGHT_GRAY, Color.orange, Color.pink, Color.white};
    
            private Color color;
    
            public Ball() {
                timer.start();
    
            }
    
            public void setColor(Color color) {
                this.color = color;
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                for (Ball ball : ballList) {
                    // Check boundaries
                    if (ball.x < radius) ball.dx = Math.abs(ball.dx);
                    if (ball.x > getWidth() - radius) ball.dx = -Math.abs(ball.dx);
                    if (ball.y < radius) ball.dy = Math.abs(dy);
                    if (ball.y > getHeight() - radius) ball.dy = -Math.abs(ball.dy);
    
                    // Adjust ball position
                    ball.x += ball.dx;
                    ball.y += ball.dy;
                    g.setColor(ball.color);
                    g.fillOval(ball.x - radius, ball.y - radius, radius * 2, radius * 2);
                }//close for loop
            }//close paint method
    
    
            private class TimerListener implements ActionListener {
                /**
                 * Handle the action event
                 */
                public void actionPerformed(ActionEvent e) {
    
                    repaint();
    
                }
            }//close timer class
    
            public void suspend() {
                timer.stop(); // Suspend timer
            }
    
            public void resume() {
                timer.start(); // Resume timer
            }
    
            public void setDelay(int delay) {
                this.delay = delay;
                timer.setDelay(delay); //Adjusts delay per slider
            }
    
            public void increment() {
    
                int colorPaletteIndex = (int) (Math.random() * colorPalette.length);
                Ball tempBall = new Ball();
                tempBall.setColor(colorPalette[colorPaletteIndex]);
                ballList.add(tempBall);
    
            }
    
            public void decrement() {
                int lastBall = ballList.size();
                if (lastBall >= 1)
                    ballList.remove(lastBall - 1);
            }
        }//close ball class
    }
    Last edited by kaplis; 06-10-2012 at 09:46 AM.

Similar Threads

  1. how to add more balls via user input
    By Johnny2009 in forum New To Java
    Replies: 22
    Last Post: 12-12-2011, 02:33 AM
  2. help! bouncing ball program
    By gryd00 in forum New To Java
    Replies: 2
    Last Post: 05-10-2011, 07:58 AM
  3. ArrayList and Bouncing Balls
    By jamie23 in forum AWT / Swing
    Replies: 1
    Last Post: 02-20-2011, 06:54 PM
  4. Multiple bouncing balls
    By Algar in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 08:35 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 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
  •