複合ツリーがあります。オーバーライドして、このツリーをJPanel
widthGraphics
オブジェクトに描画しましたpaintComponent(Graphics gr)
。私の問題は、クリックされたコンポーネントにどのようにアクセスできますか?
私は些細で悪い解決策しか思いつかなかったので、あなたに頼ります。前もって感謝します!
複合ツリーがあります。オーバーライドして、このツリーをJPanel
widthGraphics
オブジェクトに描画しましたpaintComponent(Graphics gr)
。私の問題は、クリックされたコンポーネントにどのようにアクセスできますか?
私は些細で悪い解決策しか思いつかなかったので、あなたに頼ります。前もって感謝します!
描いた形は、保存する必要があります。マウス クリック イベントを受け取った場合:
public void mouseClicked(MouseEvent e) {
あなたはからxとyを持っています
e.getPoint().getX()
e.getPoint().getY()
次に、シェイプをトラバースして、シェイプに上記のポイントが含まれているかどうかを確認できます。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);