Hi
I have a UI which has a panel and you can use your mouse for puting some points on it and it is its code:
double x = evt.getX();
double y = evt.getY() + 20;
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillOval((int) x, (int) y, 10, 10);
Point point = new Point(x, y);
pointList.add(point);
as you see my “x” field and “y” field has double type but when i print those points ,their value will be int ,how can i keep their value and use them in my functions with double type.
when I print them :
[120.0 ,134.0]
[345.0,785.0]
The positions on the panel are always going to be integers, because that’s what getX and getY return. You can’t click on half a pixel. 😉
What you have stored is actually a double. That’s why it says “120.0” rather than just “120”. If you store an integer into a floating point type (like a double), then it gets stored as the floating point equivalent of that integer (which is the same as putting “.0” at the end). Because the value you are storing into it will always be an integer, you’re always going to end up with a whole number in your doubles (unless you do some other mathematical processing on them which causes them to change)