Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.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_Swing_Jpanel_Mouseevent_Paintcomponent - Fatal编程技术网

Java 从面板绘制的复合树中检索单击的组件

Java 从面板绘制的复合树中检索单击的组件,java,swing,jpanel,mouseevent,paintcomponent,Java,Swing,Jpanel,Mouseevent,Paintcomponent,我有一棵复合树。我通过覆盖paintComponent(Graphics gr)将此树绘制到JPanelwidthGraphics对象。我的问题是:如何访问单击的组件? 我只想出了一些琐碎和糟糕的解决办法,所以我才求助于你。提前谢谢 您绘制的形状需要存储。 当您收到鼠标单击事件时: public void mouseClicked(MouseEvent e) { 你会有x和y e.getPoint().getX() e.getPoint().getY() 然后可以遍历形状,查

我有一棵复合树。我通过覆盖
paintComponent(Graphics gr)
将此树绘制到
JPanel
width
Graphics
对象。我的问题是:如何访问单击的组件?


我只想出了一些琐碎和糟糕的解决办法,所以我才求助于你。提前谢谢

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

public void mouseClicked(MouseEvent e) {
你会有x和y

    e.getPoint().getX()
    e.getPoint().getY()
然后可以遍历形状,查看形状是否包含上面的点。从形状的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);

谢谢这似乎是一个可实施的解决方案。我在周末尝试了这个,然后再回复你。我刚刚实现了你的解决方案,效果非常好,谢谢!杰出的祝其他人好运。