0

空のスペースがあるShapeオブジェクトを作成する必要があります。以下に例を示します。 ここに画像の説明を入力してください

現在、空白は別の白い長方形ですが、実際には空にしておきたいと思います。青い長方形の後ろを参照してください。それは可能ですか?この効果を実装するための解決策は見つかりませんでした。これは私のコードです:

package StackPaneTest;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;


public class StackedPaneController extends Application
        implements Initializable {


    @FXML //  fx:id="panelBlock"
    private StackPane panelBlock; // Value injected by FXMLLoader
    @FXML
    private Rectangle body;
    @FXML
    private Rectangle redBox;

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

    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        Shape newShape = Shape.subtract(body, redBox);
        panelBlock.getChildren().add(0, newShape);
        panelBlock.getChildren().remove(body);

    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("The test");
        Group root = new Group();

        try {
            root.getChildren().add((Node) FXMLLoader.load(getClass().getResource("StackedPaneBlock.fxml")));
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

そしてFXMLファイル:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="StackPaneTest.StackedPaneController">
  <children>
    <StackPane fx:id="panelBlock" layoutX="136.0" layoutY="74.0" prefHeight="126.0" prefWidth="185.0">
      <children>
        <Rectangle fx:id="body" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="200.0" stroke="BLACK" strokeType="INSIDE" width="200.0" />
        <Rectangle fx:id="redBox" arcHeight="5.0" arcWidth="5.0" fill="RED" height="48.99658203125" stroke="BLACK" strokeType="INSIDE" width="59.0" />
      </children>
    </StackPane>
  </children>
</AnchorPane>

私のコードの出力は次のとおりです。 ここに画像の説明を入力してください

ご覧のとおり、減算された形状は元の位置から移動しています。これをFXMLで機能させる必要があります。

4

1 に答える 1

2

Shape.subtractは正常に機能するはずです:

public class Test extends Application {
    @Override
    public void start(final Stage stage) throws Exception {
        final Rectangle outer = new Rectangle(0,0,200,200);
        final Rectangle inner = new Rectangle(50,50,100,100);

        final Shape result = Shape.subtract(outer, inner);
        result.setFill(Color.DODGERBLUE);

        final Group group = new Group();
        group.getChildren().add(result);

        stage.setScene(new Scene(group));
        stage.show();
    }

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

上記のリストは、希望する結果を生成します。うまくいかない場合は、特定のエラーメッセージ/結果のスクリーンショットを投稿してください。

于 2013-03-24T18:47:41.240 に答える