Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Help with bouncing ball

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with bouncing ball

    Hello everyone I am having a hard time bouncing a ball I cant seem to get the collision correct and the ball is moving way to fast.Any help would be greatly appreciated.Here is my code.
    import java.awt.*;
    import javax.swing.*;
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/31/12
     * Time: 4:41 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Ball extends JPanel {
        private Color _color;
        private int _radius;
        private int _xSpeed;
        private int _ySpeed;
        private int _xPoint;
        private int _yPoint;
        private double _direction;
     
        public Ball()
        {
            _color = Color.YELLOW;
            _radius = 5;
            _direction = Math.random() * 360;
        }
        public Color getColor()
        {
            return _color;
        }
        public double getDirection()
        {
            return _direction;
        }
        public int getXSpeed()
        {
            return _xSpeed;
        }
        public void setXSpeed(int xSpeed)
        {
            _xSpeed = xSpeed;
        }
        public int getYSpeed()
        {
            return _ySpeed;
        }
        public  void setYSpeed( int ySpeed)
        {
            _ySpeed = ySpeed;
        }
        public int getXPoint()
        {
            _xPoint = (int)(Math.random() * 800 +5);
            return _xPoint;
        }
        public int getYPoint()
        {
            _yPoint = (int)(Math.random() * 800 +5);
            return _yPoint;
        }
        public void setXPoint(int xPoint)
        {
            _xPoint = xPoint;
        }
        public void setYPoint(int yPoint)
        {
            _yPoint = yPoint;
        }
        public void setColor(Color color)
        {
            _color = color;
        }
        public void setDirection(double direction)
        {
            _direction = direction;
        }
        public void setRadius(int radius)
        {
            _radius = radius;
        }
        public int getRadius()
        {
            return _radius;
        }
        protected void paintComponent(Graphics graphics)
        {
            graphics.setColor(getColor());
            graphics.fillOval(0,0, getRadius() * 2, getRadius() * 2);
            graphics.setColor(Color.BLACK);
            graphics.drawOval(0,0,getRadius() *2, getRadius() * 2);
        }
    }
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/31/12
     * Time: 6:34 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Board extends JFrame {
     
        JPanel panel1 = new JPanel();
        Ball ball = new Ball();
        Timer timer = new Timer(10,new TimerEventHandler());
     
        int xPoint =0;
        int yPoint =0;
        Board()
        {
            timer.start();
            this.setLayout(null);
            panel1.setLayout(null);
            panel1.setBounds(0,0,800,800);
            this.add(panel1);
            panel1.add(ball);
            panel1.setBackground(Color.BLACK);
            if(ballHitsLeftOrRight() || ballHitsTopOrBottom())
            {
                ballBounce();
            }
        }
        public boolean ballHitsTopOrBottom()
        {
            if(ball.getY() >= 800)
                return true;
            else if(ball.getY() <= 0)
                return true;
            else return false;
        }
        public boolean ballHitsLeftOrRight()
        {
            if(ball.getX() >= 800)
                return true;
            else if(ball.getY() <= 0)
                return true;
            else
                return false;
        }
        public void ballBounce()
        {
            if(ballHitsLeftOrRight())
            {
                ball.setXSpeed(-xPoint);
            }
            if(ballHitsTopOrBottom())
            {
                ball.setYSpeed(-yPoint);
            }
        }    
        class TimerEventHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent actionEvent)
            {
                ball.setBounds(ball.getXPoint() + ball.getXSpeed(),ball.getYPoint() + ball.getYSpeed(),15,15);
                repaint();
            }
     
        }
     }
    import javax.swing.*;
     
    /**
     * Created by IntelliJ IDEA.
     * User: aortell24
     * Date: 7/31/12
     * Time: 6:42 PM
     * To change this template use File | Settings | File Templates.
     */
    public class Application {
        public static void main(String[] args)
        {
            Board board = new Board();
            board.setSize(800, 600);
            board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            board.setLocationRelativeTo(null);
            board.setVisible(true);
     
        }
    }


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with bouncing ball

    To slow it you could always make the x and y positions a double or float i suppose and do the same for the x and y point. then multiply them by 0.1 or something. It should go slower now.
    I dont recommend on using a timer though.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with bouncing ball

    Also another tip, instead of writing a "_" in front of every variable the class has, you could also use "this." makes everything a bit easier in my opinion. Example:
    private int speed = 0;
    public void setSpeed(int speed){
      this.speed = speed;
    }

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    Thanks alot guys that is a old habit my teacher told me he puts '_' in front of private data fields.Thanks for the help.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing ball

    Try debugging the code by printing out the x,y locations every time they change to see how far the ball moves each time it is moved.
    How often is the ball's location changed? Can you change how frequently its position is changed to make it move slower?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with bouncing ball

    Owh, well mine thought me to use this. by this. you refer to a private data field. Also i'm not quite inderstanding your code. Do you want a simple ball bouncing on the screen? If so, look at this line:
     ball.setBounds(ball.getXPoint() + ball.getXSpeed(),ball.getYPoint() + ball.getYSpeed(),15,15);
    And then specifically what ball.getXPoint and ball.getYPoint do.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    My project is to create an instance of the ball for every client that is connected to a server and the client should have a way to display and change the color , speed and direction of the ball.

  8. #8
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    But yes I just need a bouncing ball fro every client connected .

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with bouncing ball

    Try to get just 1 ball able to bounce and move properly. If you follow Norm's tips on his post, and then read what i said, you should be able to make a conclusion on what is going wrong .

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing ball

    Who wrote the code that changes the position (x,y values) of the ball?
    You need to look at that code to see what is happening.

    Also look at the code the controls how often the position is changed. (Hint: that's the timer)
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    I wrote all this.Thanks for the help.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing ball

    Can you explain what the code in the getXPoint() and getYPoint() methods do and how that will effect where the ball is located on the screen?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    I was trying to keep the ball inside the panel which is 800 by 800 but I though that it was moving to fast so I changed it to 80 but that did not help .

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing ball

    Can you explain what the code in the getXPoint() and getYPoint() methods do and how that will effect where the ball is located on the screen?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    I though that by giving it random number in between 0 and 800 it would position the ball at random postions inside the panel which is 800 x 800

  16. #16
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with bouncing ball

    That's true indeed. Now look at the line i pointed out. It will execute that every 10 ms i believe. so every once in a very short while it will execute ball.getXPoint() and ball.getYPoint(). And those function position the ball at random position inside the panel. All the time. You should give it a random position only at the initiation of the ball, so in the constructor for example .

  17. #17
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    So, I move that line to the construtor and just update the speed every timer tick?

  18. #18
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with bouncing ball

    You could update the speed every timer tick, but why not update the position every tick?
    make something like an update function. something along the lines of:
    public void update(){
       x+=xspeed;
       y+=yspeed;
    }
    And you change the speed everytime it hits a wall. All you have to do then is execute the update function everytime the timer finishes.

  19. #19
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    Oh got ya Thanks alot

  20. #20
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    XPoint and Ypoint keep printing out 0 I dont understand how they can be 0 and the ball is moving so fast I can bearly see it.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing ball

    How frequently is the location being updated? How much are the x and y values changing on each update?
    Can you change either of those to slow down the movement?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    I got it I finally got it ! now I just need to keep it in the panel. Thanks so much!

  23. #23
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing ball

    I have the perfect speed now I just need to keep it on the panel it goes right of the panel as soon as it makes it across the panel but the speed is perfect.

Similar Threads

  1. Has the ball hit my paddle?
    By JakkyD in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 3rd, 2012, 01:16 PM
  2. 2D Ball Animation
    By Deidelus in forum AWT / Java Swing
    Replies: 6
    Last Post: November 20th, 2011, 05:07 PM
  3. Bouncing Balls
    By roza in forum Threads
    Replies: 6
    Last Post: June 7th, 2011, 10:10 AM
  4. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM
  5. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 03:35 PM