【问题标题】:Javafx - Cursor intersect shapeJavafx - 光标相交形状
【发布时间】:2014-01-08 02:56:21
【问题描述】:

我在板上有一些对象,我想通过坐标获取这些对象的索引。 我尝试制作一个MouseEvent 处理程序并将getBoundInParent()MouseInfo.getPointerInfo().getLocation() 结合使用,但没有成功。这些方法给了我不同的坐标,无法匹配。

我应该通过光标的坐标制作一个矩形并使用getBoundInParent().intersects 方法吗?

有什么建议吗?

【问题讨论】:

    标签: object javafx mouseevent shape intersect


    【解决方案1】:

    解决方案

    在每个形状上,提供setOnMouseEnteredsetOnMouseExited 处理程序来捕捉鼠标进入和退出事件并记录鼠标所在形状的索引。

    假设

    我假设您需要与光标热点相交(例如鼠标指针箭头的尖端),而不是光标形状或光标的矩形边界(因为热点的相交是光标工作的标准方式)。

    示例应用程序输出

    • 当您将鼠标悬停在圆圈上时,该圆圈将突出显示并显示该圆圈的索引 (1)
    • 当您将鼠标悬停在矩形上时,矩形将突出显示并且矩形的索引 (2) 将显示。
    • 当您没有将鼠标悬停在任何一个形状上时,这两个形状都不会突出显示,也不会显示任何索引。

    示例代码

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.effect.DropShadow;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.HBox;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.*;
    import javafx.stage.Stage;
    
    public class ShapeIntersector extends Application {
        private static final Shape[] shapes = {
                new Circle(50, Color.AQUAMARINE),
                new Rectangle(100, 100, Color.PALEGREEN)
        };
    
        private static final DropShadow highlight =
                new DropShadow(20, Color.GOLDENROD);
    
        @Override
        public void start(Stage stage) throws Exception {
            HBox layout = new HBox(40);
            layout.setPadding(new Insets(30));
            layout.setAlignment(Pos.CENTER);
    
            Label highlightedShapeLabel = new Label(" ");
            highlightedShapeLabel.setStyle(
                    "-fx-font-family: monospace; -fx-font-size: 80px; -fx-text-fill: olive"
            );
    
            for (Shape shape: shapes) {
                layout.getChildren().add(shape);
    
                shape.setOnMouseEntered(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        shape.setEffect(highlight);
                        int idx = layout.getChildren().indexOf(shape) + 1;
                        highlightedShapeLabel.setText(
                                "" + idx
                        );
                    }
                });
    
                shape.setOnMouseExited(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        shape.setEffect(null);
                        highlightedShapeLabel.setText(" ");
                    }
                });
            }
    
            layout.getChildren().add(highlightedShapeLabel);
    
            stage.setScene(new Scene(layout));
            stage.show();
        }
    
        public static void main(String[] args) { launch(args); }
    }
    

    【讨论】:

    • 比我想象的要简单,这正是我所需要的。谢谢 ! :D
    猜你喜欢
    • 2011-10-13
    • 1970-01-01
    • 2015-06-19
    • 2012-12-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多