# Generating coordinates for my ball

• 02-25-2012, 04:10 PM
jazzermonty
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:
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?

• 02-25-2012, 06:25 PM
Norm
Re: Generating coordinates for my ball
Quote:

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.
• 02-25-2012, 06:31 PM
jazzermonty
Re: Generating coordinates for my ball
HI Norm

I think I got it.
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
• 02-25-2012, 06:43 PM
Norm
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.
• 02-25-2012, 06:45 PM
jazzermonty
Re: Generating coordinates for my ball
Thanks for the advice. Will do just that.

Cheers