javafx使用复杂形状的剪辑蒙版

时间:2012-08-03 15:11:31

标签: java javafx-2

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

我的代码是:

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();

}

1 个答案:

答案 0 :(得分: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();
    }