Results 1 to 3 of 3
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default Need help making ball move and bounce off of sides of JPanel

    I'm currently working on a program that creates a ball of random color. I'm trying to make the ball move around the JPanel it is contained in and if it hits a wall it should bounce off of it. I have been able to draw the ball and make it fill with a random color. However, I cannot get it to move around and bounce off the walls. Below is the code for the class that creates the ball. Should I possible be using multithreads? Any help would be appreciated.

    Thanks

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Ball extends JPanel{
        
        Graphics g;
        int rval; // red color value
        int gval; // green color value
        int bval; // blue color value
        private int x = 1;
        private int y = 1;
        private int dx = 2;
        private int dy = 2;
        
        public void paintComponent(Graphics g){
            
            for(int counter = 0; counter < 100; counter++){
                // randomly chooses red, green and blue values changing color of ball each time
                rval = (int)Math.floor(Math.random() * 256);
                gval = (int)Math.floor(Math.random() * 256);
                bval = (int)Math.floor(Math.random() * 256);
                
                
                super.paintComponent(g);
                g.drawOval(0,0,30,30);   // draws circle
                g.setColor(new Color(rval,gval,bval)); // takes random numbers from above and creates RGB color value to be displayed
                g.fillOval(x,y,30,30); // adds color to circle
                move(g);
                
                
            }
        } // end paintComponent
        
        public void move(Graphics g){
            
            g.fillOval(x, y, 30, 30);
            x += dx;
            y += dy;
            if(x < 0){
                x = 0;
                dx = -dx;
            }
            if (x + 30 >= 400) {
                x = 400 - 30;
                dx = -dx;
            }
            if (y < 0) {
                y = 0;
                dy = -dy;
            }
            if (y + 30 >= 400) {
                y = 400 - 30;
                dy = -dy;
            }
            g.fillOval(x, y, 30, 30);
            g.dispose();
            
        }   
    } // end Ball class

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class BallMoves extends JPanel implements Runnable {
        Color color = Color.red;
        int dia = 30;
        long delay = 40;
        private int x = 1;
        private int y = 1;
        private int dx = 3;
        private int dy = 2;
        
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g.setColor(color); 
            g.fillOval(x,y,30,30);   // adds color to circle
            g.setColor(Color.black);
            g2.drawOval(x,y,30,30);   // draws circle
        }
    
        public void run() {
            while(isVisible()) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    System.out.println("interrupted");
                }
                move();
                repaint();
            }
        }
        
        public void move() {
            if(x + dx < 0 || x + dia + dx > getWidth()) {
                dx *= -1;
                color = getColor();
            }
            if(y + dy < 0 || y + dia + dy > getHeight()) {
                dy *= -1;
                color = getColor();
            }
            x += dx;
            y += dy;
        }
    
        private Color getColor() {
            int rval = (int)Math.floor(Math.random() * 256);
            int gval = (int)Math.floor(Math.random() * 256);
            int bval = (int)Math.floor(Math.random() * 256);
            return new Color(rval, gval, bval);
       }
    
        private void start() {
            while(!isVisible()) {
                try {
                    Thread.sleep(25);
                } catch(InterruptedException e) {
                    System.exit(1);
                }
            }
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) {
            BallMoves test = new BallMoves();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

  3. #3
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Thanks Hardwired for the help. Animating objects is very new to me. This is the first time I had to do in programming. I appreciate the time you have taken to help out a beginner like myself.

Similar Threads

  1. Help with Move Shape
    By romina in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2010, 03:25 AM
  2. bouncing ball issue
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 03:48 AM
  3. How do I make My ball to move randomly?
    By whdbstjr90 in forum New To Java
    Replies: 4
    Last Post: 12-31-2007, 05:32 PM
  4. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM
  5. Bouncing Ball Just Suddenly Stops Mid Bounce
    By adlb1300 in forum Java 2D
    Replies: 1
    Last Post: 12-03-2007, 02:58 PM

Posting Permissions

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