繁体   English   中英

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

[英]Getting center location of square when mouse is clicked? Java

这更多是一个概念上的问题。 假设您有一个正方形数组,每个正方形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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM