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

122 lines
3.4 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;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
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;
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 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;
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 00:43:06 +02:00
StackPane pane = new StackPane();
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
primaryStage.setTitle("jFx3Dtest");
Scene scene = new Scene(pane, 600, 600, true,
SceneAntialiasing.BALANCED);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
Box myBox = new Box(300, 300, 300);
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);
pointLightSky.setRotate(90);
PointLight pointLightGround = new PointLight(Color.WHITE);
pointLightSky.setTranslateX(0);
pointLightSky.setTranslateY(600);
pointLightSky.setTranslateZ(0);
pointLightSky.setRotate(90);
2019-03-30 22:31:04 +01:00
2019-04-01 00:43:06 +02:00
pane.getChildren().add(pointLightFront);
pane.getChildren().add(pointLightSky);
pane.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 00:43:06 +02:00
pane.getChildren().add(myBox);
primaryStage.setScene(scene);
primaryStage.show();
2019-03-30 22:31:04 +01:00
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
}