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
widthGraphics
对象。我的问题是:如何访问单击的组件?
我只想出了一些琐碎和糟糕的解决办法,所以我才求助于你。提前谢谢 您绘制的形状需要存储。 当您收到鼠标单击事件时:
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);
谢谢这似乎是一个可实施的解决方案。我在周末尝试了这个,然后再回复你。我刚刚实现了你的解决方案,效果非常好,谢谢!杰出的祝其他人好运。