Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个在半个 JPanel 上渲染的 4x4 棋盘(分辨率 400x400)的缓冲图像。是否可以在不手动操作的情况下找到每个方角的坐标?我在 JPanel 上使用绝对定位,它是除 Frame 之外的唯一容器
如果您有一个 4 x 4 的棋盘格,即 400 x 400,那么每个正方形是 100 x 100 像素。
当您像这样构建一个时,您在构建时为每个正方形BufferedImage保存一个。Rectangle
BufferedImage
Rectangle
这样,当您稍后单击鼠标时,可以使用 的contains方法Rectangle来确定单击了哪个方格。
contains
你不应该使用绝对定位。如果你的棋盘占据了JPanel的一半,FlowLayout或者BoxLayout运行良好。
FlowLayout
BoxLayout