Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击鼠标时获取正方形的中心位置?JAVA_Java_Arrays_Graphics_Paint - Fatal编程技术网

单击鼠标时获取正方形的中心位置?JAVA

单击鼠标时获取正方形的中心位置?JAVA,java,arrays,graphics,paint,Java,Arrays,Graphics,Paint,这是一个概念性的问题。假设你有一个正方形阵列,每个正方形40像素乘40像素。也可以说你点击了其中一个。如何使另一个对象显示在单击的框的中心,而不是鼠标单击的确切位置? 你会用某种补偿吗?我真的很难理解如何根据鼠标点击来确定正方形的中心。假设您可以计算点击框的位置(mousex/40用于列,mousey/40用于行),那么您只需要计算对象的中心位置 通常是这样的 int x = (parentWidth - childWidth) / 2; int y = (parentWidth - child

这是一个概念性的问题。假设你有一个正方形阵列,每个正方形40像素乘40像素。也可以说你点击了其中一个。如何使另一个对象显示在单击的框的中心,而不是鼠标单击的确切位置?
你会用某种补偿吗?我真的很难理解如何根据鼠标点击来确定正方形的中心。

假设您可以计算点击框的位置(
mousex/40
用于列,
mousey/40
用于行),那么您只需要计算对象的中心位置

通常是这样的

int x = (parentWidth - childWidth) / 2;
int y = (parentWidth - childWidth) / 2;
将为您提供子对象在父对象中的中心位置。然后,您只需应用相关框的偏移量

int x = xOffset + ((parentWidth - childWidth) / 2);
int y = yOffset + ((parentWidth - childWidth) / 2);

每个正方形有什么尺寸?左上角的坐标加上边长?还是两个对角的坐标?