jFx3DTest/src/main/java/com/ThreeDtest/application/Main.java

171 lines
4.9 KiB
Java
Raw Normal View History

2019-03-30 22:31:04 +01:00
package main.java.com.ThreeDtest.application;
2019-04-01 00:43:06 +02:00
import javafx.animation.Interpolator;
import javafx.animation.RotateTransition;
2019-03-30 22:31:04 +01:00
import javafx.application.Application;
2019-04-01 22:12:19 +02:00
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
2019-03-30 22:31:04 +01:00
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
2019-04-01 22:12:19 +02:00
import javafx.geometry.Insets;
2019-04-01 00:43:06 +02:00
import javafx.geometry.Pos;
2019-03-30 22:31:04 +01:00
import javafx.stage.Stage;
2019-04-01 00:43:06 +02:00
import javafx.util.Duration;
2019-04-01 22:12:19 +02:00
import javafx.scene.Group;
2019-04-01 00:43:06 +02:00
import javafx.scene.Node;
import javafx.scene.PointLight;
2019-03-30 22:31:04 +01:00
import javafx.scene.Scene;
2019-04-01 00:43:06 +02:00
import javafx.scene.SceneAntialiasing;
2019-03-30 22:31:04 +01:00
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
2019-04-01 22:12:19 +02:00
import javafx.scene.control.Slider;
2019-04-01 00:43:06 +02:00
import javafx.scene.image.Image;
2019-03-30 22:31:04 +01:00
import javafx.scene.layout.AnchorPane;
2019-04-01 00:43:06 +02:00
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
2019-04-01 22:12:19 +02:00
import javafx.scene.layout.BorderPane;
2019-04-01 00:43:06 +02:00
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
2019-03-30 22:31:04 +01:00
public class Main extends Application
{
2019-04-01 00:43:06 +02:00
@Override
public void start(Stage primaryStage)
{
try {
2019-03-30 22:31:04 +01:00
2019-04-01 22:12:19 +02:00
BorderPane demoPane = new BorderPane(); // Pane for this demo
2019-04-01 00:43:06 +02:00
primaryStage.setTitle("jFx3Dtest");
2019-04-01 22:12:19 +02:00
Scene scene = new Scene(demoPane, 600, 600, true,
2019-04-01 00:43:06 +02:00
SceneAntialiasing.BALANCED);
2019-03-30 22:31:04 +01:00
2019-04-01 22:12:19 +02:00
StackPane previewPane = new StackPane(); // Stackpane in
// textur3dPreview
previewPane.setMaxWidth(300);
previewPane.setMaxHeight(300);
previewPane.setStyle(
"-fx-background-color: #383838;-fx-border-color: red;");
Box myBox = new Box(100, 100, 100);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
final String DIFFUSE_MAP = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/D%C3%BClmen%2C_Kreuzkapelle_--_2014_--_2731.jpg/640px-D%C3%BClmen%2C_Kreuzkapelle_--_2014_--_2731.jpg";
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
Image texture = new Image(DIFFUSE_MAP);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
PhongMaterial textureMaterial = new PhongMaterial();
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
textureMaterial.setDiffuseMap(texture);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
myBox.setMaterial(textureMaterial);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
PointLight pointLightFront = new PointLight(Color.WHITE);
pointLightFront.setTranslateX(100);
pointLightFront.setTranslateY(100);
pointLightFront.setTranslateZ(-300);
pointLightFront.setRotate(90);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
PointLight pointLightSky = new PointLight(Color.WHITE);
pointLightSky.setTranslateX(0);
pointLightSky.setTranslateY(-600);
pointLightSky.setTranslateZ(0);
2019-04-01 22:12:19 +02:00
pointLightSky.setRotate(90);
2019-04-01 00:43:06 +02:00
PointLight pointLightGround = new PointLight(Color.WHITE);
pointLightSky.setTranslateX(0);
pointLightSky.setTranslateY(600);
pointLightSky.setTranslateZ(0);
2019-04-01 22:12:19 +02:00
pointLightSky.setRotate(90);
2019-03-30 22:31:04 +01:00
2019-04-01 22:12:19 +02:00
previewPane.getChildren().add(pointLightFront);
previewPane.getChildren().add(pointLightSky);
previewPane.getChildren().add(pointLightGround);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
StackPane.setAlignment(pointLightFront, Pos.CENTER);
StackPane.setAlignment(pointLightSky, Pos.CENTER);
StackPane.setAlignment(pointLightGround, Pos.CENTER);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
Rotate ryBox = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
Rotate rzBox = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
rxBox.setAngle(30);
ryBox.setAngle(50);
rzBox.setAngle(30);
myBox.getTransforms().addAll(rxBox, ryBox, rzBox);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
StackPane.setAlignment(myBox, Pos.CENTER);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
rotateAroundYAxis(myBox).play();
2019-03-30 22:31:04 +01:00
2019-04-01 22:12:19 +02:00
previewPane.getChildren().add(myBox);
// demoPane.setCenter(previewPane);
Slider resSlider = new Slider();
resSlider.setMin(0);
resSlider.setMax(400);
resSlider.setValue(200);
resSlider.setBlockIncrement(10);
resSlider.setShowTickLabels(true);
resSlider.setShowTickMarks(true);
resSlider.setMajorTickUnit(50);
resSlider.setMinorTickCount(5);
resSlider.setBlockIncrement(10);
// Adding Listener to value property.
resSlider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, //
Number oldValue, Number newValue)
{
System.out.println("New value: " + newValue);
}
});
BorderPane.setAlignment(resSlider, Pos.TOP_LEFT);
BorderPane.setMargin(resSlider, new Insets(12, 12, 12, 12));
demoPane.setBottom(resSlider);
StackPane.setAlignment(previewPane, Pos.CENTER);
2019-04-01 00:43:06 +02:00
primaryStage.setScene(scene);
primaryStage.show();
2019-03-30 22:31:04 +01:00
2019-04-01 22:12:19 +02:00
System.out.println(previewPane.getHeight());
System.out.println(previewPane.getWidth());
2019-04-01 00:43:06 +02:00
} catch (Exception e) {
e.printStackTrace();
}
2019-03-30 22:31:04 +01:00
}
2019-04-01 00:43:06 +02:00
private RotateTransition rotateAroundYAxis(Node node)
2019-03-30 22:31:04 +01:00
{
2019-04-01 00:43:06 +02:00
RotateTransition rotate = new RotateTransition(Duration.seconds(36),
node);
rotate.setAxis(Rotate.Y_AXIS);
rotate.setFromAngle(360);
rotate.setToAngle(0);
rotate.setInterpolator(Interpolator.LINEAR);
rotate.setCycleCount(RotateTransition.INDEFINITE);
return rotate;
2019-03-30 22:31:04 +01:00
}
public static void main(String[] args)
{
launch(args);
}
2019-04-01 00:43:06 +02:00
}