Results 1 to 5 of 5
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Generating coordinates for my ball

    HI folks

    Ok, so I can draw myball on the JPanel no worries. Now I want it to move about the frame. I want to create a method that will give me new x and y coordinates for my ball then I can use these to redraw, but not sure how to generate the numbers. Something like this:
    Java Code:
     public void move()
            {
             pos = new Point();
             while(true)
                      {
             if (pos.x <= getWidth()-DIAMATER)
             {
                 pos.translate(CHANGE + CHANGE, CHANGE + CHANGE);
                 System.out.println(" going up" +pos);
             }
             if (pos.x <= getHeight()-DIAMATER)
             {
                 pos.translate(CHANGE - CHANGE, CHANGE - CHANGE);
                 System.out.println("going down" + pos);
             }
    
                }
            
            }
    Which I know is wrong, but would be greatful if someone could give a hint on how to change the values of pos within the bounds of the frame dimentions?

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Generating coordinates for my ball

    not sure how to generate the numbers
    Doesn't that depend on how you want to ball to move?
    If the ball is currently at an x,y position, what are the next values of x,y that you want to be used to show the ball at the new position?
    Also you need to test if the new position is within the bounds that you want it to be shown in.

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Generating coordinates for my ball

    HI Norm

    I think I got it.
    Java Code:
        private int posX = 200;
        private int posY = 200;
        private double offsetX;
        private double offsetY;
        private final int DIAMATER = 60;
    
    
    
    <snip>
    
    
    
    public void move()
    
    if(posX <=0 || (posX + DIAMATER)> getWidth())
                {
                    offsetX = -1*offsetX;
                }
                if(posY <=0 || (posY + DIAMATER)> getHeight())
                {
                    offsetY = -1*offsetY;
                }
                posX +=offsetX;
                posY +=offsetY;
    So now the ball bounces around nicely. Need to get is to stop now if it hit's the east boundry.

    Thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Generating coordinates for my ball

    Test if the new x value will be less than 0 and don't use it if it is.

  5. #5
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Re: Generating coordinates for my ball

    Thanks for the advice. Will do just that.

    Cheers

Similar Threads

  1. Cant create more than one ball
    By vettera in forum AWT / Swing
    Replies: 5
    Last Post: 09-17-2011, 06:26 PM
  2. Ball program
    By codeStone in forum Advanced Java
    Replies: 10
    Last Post: 02-25-2011, 02:32 PM
  3. Box Ball Java
    By fcgb9115 in forum New To Java
    Replies: 1
    Last Post: 04-19-2010, 03:33 AM
  4. [SOLVED] Bouncing ball with polar coordinates
    By Ypsilon IV in forum New To Java
    Replies: 4
    Last Post: 04-22-2009, 01:19 AM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 10:08 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
  •