I have three points on a grid. I want my mouselistener to give me a heads up, when a point on the area within this imaginary triangle of three points has been clicked. how to do that??I dont wanna use a shape class or anything, since it is an imaginary triangle. any ideas ? thx!
问问题
42 次
1 回答
1
以下代码应该完成您正在尝试做的事情。
private static final Polygon POLY = new Polygon();
static {
POLY.addPoint(x1, y1); // first point
POLY.addPoint(x2, y2); // second point
POLY.addPoint(x3, y3); // third point
}
@Override
public void mouseClicked(final MouseEvent e) {
if (POLY.contains(e.getX(), e.getY())) {
// notify user
}
}
请注意,即使您拥有的三角形是“虚构的”,您也需要定义某种多边形。这样 Java 就知道形状内可能有什么样的点。
于 2016-07-08T13:05:35.570 回答