Rob
Rob

Reputation: 3

Point Coordinates to x and y cordinates in variables doesnt really work

i just try to program pong with the use of java swing components.

My problem ist, that i need to store the coordinates of my JButton (my Paddle) in variables to be able to manipulate the position of the Button when moving.

I tried doing it that way:

int posP1_x= paddel1.getLocation().getX(); //Error

When compiling it says, that there is a lossy conversion from double to int. (But the retrun value of getX should be int and in the Point-Class the values are also stored as ints). When i try declaring posP1_x as double and print the variables value on the console, it always prints 0.0. But when i print paddel1.getLocation().getX() directly, it works...

double posP1_x= paddel1.getLocation().getX(); //Works

System.out.println(paddel1.getLocation().getX());    //Prints double value eg 110.0

System.out.println(posP1_x);    //Prints double value  with 0. --> 0.0

What could be the solution to save JButton Coordinates in Variables.

Thank you and have a nice day

Upvotes: 0

Views: 83

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Don't use getX(). Use .x. That's it. For example:

// either
int posP1_x= (int) paddel1.getLocation().getX();

// or
int posP1_x= paddel1.getLocation().x;

More importantly, look at the relevant API before posting here. If you'd simply look at the Point API, you'd have your answer.

Upvotes: 1

Related Questions