簡體   English   中英

JavaFX將ImageView剪切到AnchorPane中的橢圓

[英]JavaFX Clipping an ImageView to an Ellipse within an AnchorPane

我一直在構建一個應用程序,該應用程序要在橢圓內顯示一個大圖像來模仿天文館的屋頂。 使用myImageView.setClip(myEllipse)將ImageView顯示的圖像剪裁為橢圓形已經可以正常工作,直到我的應用程序的最后一個版本(我要在其中添加橢圓形的AnchorPane)似乎都不喜歡在其中剪切圖像。

我對於myImageView.setClip(myEllipse)行的錯誤如下:

造成原因:

java.lang.IllegalArgumentException:節點的clip設置為不正確的值(節點已連接,node = ImageView @ a13b0a6,clip = ObjectProperty [bean:ImageView @ a13b0a6,名稱:clip,值:null])。

我了解的是我正在場景圖中創建某種循環,但我不知道在哪里。

這是我的代碼(整個接口都是“硬編碼的”)

package opinarium3;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import static javafx.scene.layout.VBox.setMargin;
import javafx.scene.shape.Ellipse;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 *
 * @author Admin
 */
public class Opinarium3 extends Application {
    private Ellipse ceiling;
    private ImageView ceiling_image;
    private VBox nav;
    private HBox buttonSet;
    private Label presentation_title;
    private TextArea info;
    private Button previous;
    private Button comment;
    private Button next;
    private double sx;
    private double sy;
    private DoubleProperty coordX = new SimpleDoubleProperty(0);
    private DoubleProperty coordY = new SimpleDoubleProperty(0);

    @Override
    public void start(Stage primaryStage) throws IOException {
        AnchorPane root = new AnchorPane();
        Scene primaryScene = new Scene(root, 900, 800);
        primaryScene.getStylesheets().add(Opinarium3.class.getResource("Opinarium3.css").toExternalForm());
        initializeCeiling(root);
        initializeNav(root);
        initializeContent();
        initializePrimaryStage(primaryStage, primaryScene);
        primaryStage.show();
    }

    private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
        primaryStage.setTitle("Planetario de San José - Cartelera de Presentaciones");
        primaryStage.setScene(primaryScene);
        primaryStage.setWidth(900);
        primaryStage.setHeight(800);
        primaryStage.minHeightProperty().setValue(800);
        primaryStage.minWidthProperty().setValue(900);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private void initializeCeiling(AnchorPane root) {
        ceiling = new Ellipse();
        ceiling.centerXProperty().bind(root.widthProperty().multiply(0.5));
        ceiling.centerYProperty().setValue(0);
        ceiling.radiusXProperty().bind(root.widthProperty().multiply(0.8));
        ceiling.radiusYProperty().bind(root.heightProperty().multiply(0.6));
        root.getChildren().add(ceiling);
    }

    private void initializeNav(AnchorPane root) {
        nav = new VBox();
        initializeControls(nav);
        AnchorPane.setBottomAnchor(nav, 20.0);
        AnchorPane.setLeftAnchor(nav, 120.0);
        AnchorPane.setRightAnchor(nav, 120.0);
        root.getChildren().add(nav);
    }

    private void initializeControls(VBox nav) {
        info = new TextArea();
        setMargin(info, new Insets(10, 0, 0, 0));
        info.setWrapText(true);
        info.setEditable(false);
        buttonSet = new HBox();
        initializeButtonSet(nav);
        presentation_title = new Label("Título de la Presentación");
        presentation_title.setId("titulo");
        nav.alignmentProperty().setValue(Pos.CENTER);
        nav.getChildren().addAll(presentation_title, info, buttonSet);
    }

    private void initializeButtonSet(VBox nav) {
        previous = new Button("<");
        comment = new Button("Doy mi opinión");
        comment.setId("comment_button");
        next = new Button(">");
        buttonSet.spacingProperty().bind(nav.widthProperty().multiply(0.15));
        buttonSet.setAlignment(Pos.CENTER);
        setMargin(buttonSet, new Insets(10, 0, 0, 0));
        buttonSet.getChildren().addAll(previous, comment, next);
    }

    private void initializeContent() throws IOException {
        try {
            BufferedReader br = new BufferedReader(new FileReader("src/opinarium3/media/joyas/joyas.txt"));
            String title = br.readLine();
            String length = br.readLine();
            String description = br.readLine();
            this.presentation_title.setText(title);
            this.info.setText("\n"+description+"\n\nDuración: "+length);
            br.close();


            ceiling_image = new ImageView(new Image(Opinarium3.class.getResourceAsStream("media/joyas/joyas.jpg")));
            ceiling_image.setClip(ceiling);
            /*
            ceiling_image.setOnMousePressed(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent t) {
                    sx = t.getSceneX() - coordX.doubleValue();
                    sy = t.getSceneY() - coordY.doubleValue();
                }
            });
            ceiling_image.setOnMouseDragged(new EventHandler<MouseEvent>(){
                @Override
                public void handle(MouseEvent t){
                    if(t.getSceneX() - sx < 0){
                        coordX.setValue(t.getSceneX() - sx);
                    }
                    if(t.getSceneY() - sy < 0){
                        coordY.setValue(t.getSceneY() - sy);
                    }
                }
            });
            ceiling_image.xProperty().bind(coordX);
            ceiling_image.yProperty().bind(coordY);
                    */
        }catch(IOException e){}
    }
}

屏幕截圖

您只能在場景中使用一次形狀,不能將其既設置為剪輯,又添加為元素的子級。

您正在將剪輯添加到場景,而不是將項目剪輯到場景。

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

import static javafx.scene.layout.VBox.setMargin;

public class Opinarium3 extends Application {
    private Ellipse ceiling;
    private ImageView ceiling_image;
    private VBox nav;
    private HBox buttonSet;
    private Label presentation_title;
    private TextArea info;
    private Button previous;
    private Button comment;
    private Button next;

    @Override
    public void start(Stage primaryStage) {
        AnchorPane root = new AnchorPane();
        Scene primaryScene = new Scene(root, 900, 800);
        initializeCeiling(root);
        initializeNav(root);
        initializeContent(root);
        initializePrimaryStage(primaryStage, primaryScene);
        primaryStage.show();
    }

    private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
        primaryStage.setTitle("Planetario de San José - Cartelera de Presentaciones");
        primaryStage.setScene(primaryScene);
        primaryStage.setWidth(900);
        primaryStage.setHeight(800);
        primaryStage.minHeightProperty().setValue(800);
        primaryStage.minWidthProperty().setValue(900);
    }

    public static void main(String[] args) {
        launch(args);
    }

    private void initializeCeiling(AnchorPane root) {
        ceiling = new Ellipse();
        ceiling.centerXProperty().bind(root.widthProperty().multiply(0.5));
        ceiling.centerYProperty().setValue(0);
        ceiling.radiusXProperty().bind(root.widthProperty().multiply(0.8));
        ceiling.radiusYProperty().bind(root.heightProperty().multiply(0.6));
    }

    private void initializeNav(AnchorPane root) {
        nav = new VBox();
        initializeControls(nav);
        AnchorPane.setBottomAnchor(nav, 20.0);
        AnchorPane.setLeftAnchor(nav, 120.0);
        AnchorPane.setRightAnchor(nav, 120.0);
        root.getChildren().add(nav);
    }

    private void initializeControls(VBox nav) {
        info = new TextArea();
        setMargin(info, new Insets(10, 0, 0, 0));
        info.setWrapText(true);
        info.setEditable(false);
        buttonSet = new HBox();
        initializeButtonSet(nav);
        presentation_title = new Label("Título de la Presentación");
        presentation_title.setId("titulo");
        nav.alignmentProperty().setValue(Pos.CENTER);
        nav.getChildren().addAll(presentation_title, info, buttonSet);
    }

    private void initializeButtonSet(VBox nav) {
        previous = new Button("<");
        comment = new Button("Doy mi opinión");
        comment.setId("comment_button");
        next = new Button(">");
        buttonSet.spacingProperty().bind(nav.widthProperty().multiply(0.15));
        buttonSet.setAlignment(Pos.CENTER);
        setMargin(buttonSet, new Insets(10, 0, 0, 0));
        buttonSet.getChildren().addAll(previous, comment, next);
    }

    private void initializeContent(AnchorPane root) {
        Image image = new Image(
                "http://takeinsocialmedia.com/wp-content/uploads/2014/05/landscape-art-painting-wallpaper-images-photos-0517193352.jpg"
        );
        ceiling_image = new ImageView(image);
        ceiling_image.setClip(ceiling);
        root.getChildren().add(ceiling_image);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM