3

我正在创建一个简单的 javafx 应用程序。我只想从两个同心圆之间提供的区域内剪辑图像,这会在鼠标移动时改变中心坐标,被剪辑的对象最初出现在定义的位置,但它不响应鼠标移动。请告诉我我做错了什么。

我的代码是:

public DoubleProperty xCordinate;
public DoubleProperty yCordinate;

@Override
public void start(Stage primaryStage) {

    Group root = new Group();
    Image image = new Image(MyClass.class
            .getResource("Water lilies.jpg").toExternalForm());
    Scene scene = new Scene(root, image.getWidth(), image.getHeight(),
            Color.WHITE);
    ImageView view = new ImageView();
    view.setImage(image);
    xCordinate = new SimpleDoubleProperty(100.0f);
    yCordinate = new SimpleDoubleProperty(100.0f);
    scene.setOnMouseMoved(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            xCordinate.set(event.getX());
            yCordinate.set(event.getY());
            System.out.println("xCordinate " + xCordinate + " yCordinate "
                    + yCordinate);
        }

    });

    Circle c1 = new Circle();
    c1.centerXProperty().bind(xCordinate);
    c1.centerYProperty().bind(yCordinate);
    c1.setRadius(50.0f);
    Circle c2 = new Circle();
    c2.centerXProperty().bind(xCordinate);
    c2.centerYProperty().bind(yCordinate);
    c2.setRadius(35.0f);

    Shape mask = Path.subtract(c1, c2);

    view.setClip(mask);
    root.getChildren().add(view);
    primaryStage.setScene(scene);
    primaryStage.show();

}
4

1 回答 1

6

您需要在每次更改圆圈位置时更新蒙版剪辑。顺便说一句不错的应用程序。

@Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Image image = new Image(MyClass.class.getResource("Water lilies.jpg").toExternalForm());
        Scene scene = new Scene(root, image.getWidth(), image.getHeight(), Color.WHITE);
        final ImageView view = new ImageView();
        view.setImage(image);

        xCordinate = new SimpleDoubleProperty(100.0f);
        yCordinate = new SimpleDoubleProperty(100.0f);


        final Circle c1 = new Circle();
        c1.centerXProperty().bind(xCordinate);
        c1.centerYProperty().bind(yCordinate);
        c1.setRadius(50.0f);

        final Circle c2 = new Circle();
        c2.centerXProperty().bind(xCordinate);
        c2.centerYProperty().bind(yCordinate);
        c2.setRadius(35.0f);

        scene.setOnMouseMoved(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xCordinate.set(event.getX());
                yCordinate.set(event.getY());
                System.out.println("xCordinate " + xCordinate + " yCordinate " + yCordinate);
                // update mask clip
                Shape mask = Path.subtract(c1, c2);
                view.setClip(mask);
            }
        });

        Shape mask = Path.subtract(c1, c2);
        view.setClip(mask);

        root.getChildren().add(view);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
于 2012-08-03T15:47:24.600 回答