# Thread: Generating coordinates for my ball

1. Member
Join Date
Jan 2011
Posts
71
Rep Power
0

## 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?

2. ## 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. Member
Join Date
Jan 2011
Posts
71
Rep Power
0

## 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. ## 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. Member
Join Date
Jan 2011
Posts
71
Rep Power
0

## Re: Generating coordinates for my ball

Thanks for the advice. Will do just that.

Cheers

#### Posting Permissions

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