0

Piccolo2D で「ホバー動作」を実装する最も簡単な方法は何ですか?

つまり、マウスカーソルが上にあるときにオブジェクトの色やスタイルを変更するには? ムーブインとムーブアウトの両方を正しく考慮する必要があります。

4

1 に答える 1

1

入力イベント ハンドラをノードに追加できます。以下は、レイヤーにアタッチしてイベントをキャプチャする基本的な例ですPBasicInputEventHandler。レイヤー内の個々のノードにイベント ハンドラーを追加することもできます。mouseEnteredmouseExited

import java.awt.Color;
import javax.swing.SwingUtilities;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolox.PFrame;

public class DemoInputHandler {

    @SuppressWarnings("serial")
    private static void createAndShowUI() {
        new PFrame() {
            @Override
            public void initialize() {
                PPath node = PPath.createRectangle(0, 0, 100, 100);
                node.setOffset(50, 50);
                node.setPaint(Color.BLUE);
                getCanvas().getLayer().addChild(node);

                node = PPath.createRectangle(0, 0, 100, 100);
                node.setOffset(200, 50);
                node.setPaint(Color.BLUE);
                getCanvas().getLayer().addChild(node);

                getCanvas().getLayer().addInputEventListener(
                        new PBasicInputEventHandler() {
                            @Override
                            public void mouseEntered(final PInputEvent event) {
                                event.getPickedNode().setPaint(Color.RED);
                            }

                            @Override
                            public void mouseExited(final PInputEvent event) {
                                event.getPickedNode().setPaint(Color.BLUE);
                            }
                        });
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}
于 2013-10-21T06:22:37.057 に答える