如何将SceneAntialiasing设置为在FXML文件中创建的SubScene?

时间:2019-07-14 23:48:16

标签: java javafx 3d fxml

我一直在用JavaFX玩3D。

我向演示应用程序中添加了SubScene和一些Box,但是Box的边缘很讨厌。如何在不使用构造函数中使用SceneAntialiasing.BALANCED的情况下平滑它们?我已经看到有可能在FXML文件中添加antiAliasing="",但是我不知道应该把什么作为参数。单词BALANCED无效。

Controller.java

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.scene.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.util.Duration;


public class Controller{

    @FXML
    private SubScene subscene;
    @FXML
    private AnchorPane pane;
    private Box box= new Box();
    private PerspectiveCamera camera = new PerspectiveCamera(true);
    private Group group = new Group();


    final Rotate rx = new Rotate(0, Rotate.X_AXIS);
    final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
    final Rotate rz = new Rotate(0, Rotate.Z_AXIS);
    private Timeline animation;



    @FXML
    void initialize() {

        box.setMaterial(new PhongMaterial(Color.ORANGE));
        box.setDepth(10);
        box.setWidth(10);
        box.setHeight(10);
        rx.setAngle(90);
        ry.setAngle(25);
        box.getTransforms().addAll(rz, ry, rx);


        group.getChildren().add(box);

        animation = new Timeline();
        animation.getKeyFrames().addAll(
                new KeyFrame(Duration.ZERO,

                        new KeyValue(box.depthProperty(), 0d),
                        new KeyValue(box.translateYProperty(),400d)),
                new KeyFrame(Duration.seconds(5),

                        new KeyValue(box.depthProperty(), 800d),
                        new KeyValue(box.translateYProperty(), 0d)));

        animation.setCycleCount(Timeline.INDEFINITE);


        camera.getTransforms().add(new Translate(0, 0, -80));
        camera.getTransforms().addAll (

                new Rotate(-35, Rotate.X_AXIS),
                new Translate(0, 0, 10)
        );

        subscene.setRoot(group);
        subscene.setCamera(camera);
        animation.play();
    }

}

main.fxml

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

<?import javafx.scene.SubScene?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Region?>

<AnchorPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="check.brakes.Controller">
   <children>
      <SubScene fx:id="subscene" height="697.0" layoutX="78.0" layoutY="54.0" width="643.0" AnchorPane.bottomAnchor="49.0" AnchorPane.leftAnchor="78.0" AnchorPane.rightAnchor="79.0" AnchorPane.topAnchor="54.0" >
        <root>
          <Region />
        </root>
      </SubScene>
      <Button layoutX="196.0" layoutY="186.0" mnemonicParsing="false" text="Button" />
      <Button fx:id="btn2" layoutX="450.0" layoutY="186.0" mnemonicParsing="false" text="Button" />
      <Label layoutX="386.0" layoutY="36.0" text="test" />
      <Label layoutX="381.0" layoutY="753.0" text="test 2" />
   </children>
</AnchorPane>

还有其他方法可以实现吗?

1 个答案:

答案 0 :(得分:0)

由于New Dataset 3: Start range (96.95, 96.98, 97, 97.005, 97.02, 97.05, 97.06, 97.095) end range (96.98, 97, 97.005, 97.02, 97.05, 97.06, 97.095, 97.1) Variable Y (NA, 1.48, 1.48, NA, 0.42, NA, 4.78, NA) Variable A (100, 100, 50, 50, 50, 10, 10, 10) Variable B (0, 0, 30, 30, 30, 30, 30, 30) 不是枚举,因此不能在属性中使用常量名称。但是,SceneAntialiasing构造函数的确使用SubScene注释了SceneAntialiasing参数,这意味着在通过FXML创建时可以将其注入。您只需要结合使用@NamedArg元素和属性即可。例如:

fx:constant

将以上内容加载:

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

<?import javafx.scene.SubScene?>
<?import javafx.scene.SceneAntialiasing?>

<SubScene xmlns="http://javafx.com/javafx/12.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <antiAliasing>
        <SceneAntialiasing fx:constant="BALANCED"/>
    </antiAliasing>
</SubScene>

导致import java.io.IOException; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.SubScene; import javafx.stage.Stage; public final class App extends Application { @Override public void start(Stage primaryStage) throws IOException { SubScene scene = FXMLLoader.load(getClass().getResource(...)); System.out.println(scene.getAntiAliasing()); Platform.exit(); } } 被打印。