0

我有一个复合树。我已经通过覆盖将这棵树绘制到一个JPanel宽度对象。我的问题是:如何访问单击了哪个组件?GraphicspaintComponent(Graphics gr)

我只想出了琐碎和糟糕的解决方案,所以这就是我求助于你的原因。提前致谢!

4

1 回答 1

2

您绘制的形状,您需要存储。当您收到鼠标点击事件时:

public void mouseClicked(MouseEvent e) {

您将拥有 x 和 y

    e.getPoint().getX()
    e.getPoint().getY()

然后,您可以遍历您的形状,并查看 Shape 是否包含上述点。来自 Shape 的 Javadoc:

/**
 * Tests if the specified coordinates are inside the boundary of the
 * <code>Shape</code>, as described by the
 * <a href="{@docRoot}/java/awt/Shape.html#def_insideness">
 * definition of insideness</a>.
 * @param x the specified X coordinate to be tested
 * @param y the specified Y coordinate to be tested
 * @return <code>true</code> if the specified coordinates are inside
 *         the <code>Shape</code> boundary; <code>false</code>
 *         otherwise.
 * @since 1.2
 */
public boolean contains(double x, double y);
于 2012-11-07T23:16:29.347 回答