own class for preview

This commit is contained in:
Hendrik Schutter 2019-04-01 22:57:19 +02:00
parent ef5a45a80d
commit 63f03b229c
3 changed files with 159 additions and 135 deletions

View File

@ -1,37 +1,10 @@
package main.java.com.ThreeDtest.application; package main.java.com.ThreeDtest.application;
import javafx.animation.Interpolator;
import javafx.animation.RotateTransition;
import javafx.application.Application; import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.PointLight;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing; import javafx.scene.SceneAntialiasing;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
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;
public class Main extends Application public class Main extends Application
{ {
@ -46,123 +19,43 @@ public class Main extends Application
Scene scene = new Scene(demoPane, 600, 600, true, Scene scene = new Scene(demoPane, 600, 600, true,
SceneAntialiasing.BALANCED); SceneAntialiasing.BALANCED);
StackPane previewPane = new StackPane(); // Stackpane in //Create new Preview
// textur3dPreview Texture3DPreview preview = new Texture3DPreview(500, 500);
// Get the generated 3D preview as StackPane
demoPane.setCenter(preview.getPreviewPane());
previewPane.setMaxWidth(300); /*
previewPane.setMaxHeight(300); *
* Slider resSlider = new Slider();
previewPane.setStyle( *
"-fx-background-color: #383838;-fx-border-color: red;"); * resSlider.setMin(0); resSlider.setMax(400); resSlider.setValue(200);
* resSlider.setBlockIncrement(10); resSlider.setShowTickLabels(true);
Box myBox = new Box(100, 100, 100); * resSlider.setShowTickMarks(true); resSlider.setMajorTickUnit(50);
* resSlider.setMinorTickCount(5); resSlider.setBlockIncrement(10);
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"; *
* // Adding Listener to value property.
Image texture = new Image(DIFFUSE_MAP); * resSlider.valueProperty().addListener(new ChangeListener<Number>() {
*
PhongMaterial textureMaterial = new PhongMaterial(); * @Override public void changed(ObservableValue<? extends Number>
* observable, // Number oldValue, Number newValue) {
textureMaterial.setDiffuseMap(texture); *
* System.out.println("New value: " + newValue); } });
myBox.setMaterial(textureMaterial); *
* BorderPane.setAlignment(resSlider, Pos.TOP_LEFT);
PointLight pointLightFront = new PointLight(Color.WHITE); * BorderPane.setMargin(resSlider, new Insets(12, 12, 12, 12));
pointLightFront.setTranslateX(100); * demoPane.setBottom(resSlider);
pointLightFront.setTranslateY(100); *
pointLightFront.setTranslateZ(-300); */
pointLightFront.setRotate(90);
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);
previewPane.getChildren().add(pointLightFront);
previewPane.getChildren().add(pointLightSky);
previewPane.getChildren().add(pointLightGround);
StackPane.setAlignment(pointLightFront, Pos.CENTER);
StackPane.setAlignment(pointLightSky, Pos.CENTER);
StackPane.setAlignment(pointLightGround, Pos.CENTER);
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);
StackPane.setAlignment(myBox, Pos.CENTER);
rotateAroundYAxis(myBox).play();
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);
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.show(); primaryStage.show();
System.out.println(previewPane.getHeight());
System.out.println(previewPane.getWidth());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private RotateTransition rotateAroundYAxis(Node node)
{
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;
}
public static void main(String[] args) public static void main(String[] args)
{ {
launch(args); launch(args);

View File

@ -0,0 +1,131 @@
package main.java.com.ThreeDtest.application;
import javafx.animation.Interpolator;
import javafx.animation.RotateTransition;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.PointLight;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.transform.Rotate;
import javafx.util.Duration;
public class Texture3DPreview
{
private StackPane previewPane;
public Texture3DPreview(int height, int width)
{
previewPane = new StackPane(); // Stackpane in
// textur3dPreview
previewPane.setMaxWidth(width);
previewPane.setMaxHeight(height);
previewPane
.setStyle("-fx-background-color: #383838;-fx-border-color: red;");
int boxSize = calcBoxSize(height, width);
Box myBox = new Box(boxSize, boxSize, boxSize);
myBox.setCullFace(CullFace.NONE);
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";
Image texture = new Image(DIFFUSE_MAP);
PhongMaterial textureMaterial = new PhongMaterial();
textureMaterial.setDiffuseMap(texture);
myBox.setMaterial(textureMaterial);
PointLight pointLightFront = new PointLight(Color.WHITE);
pointLightFront.setTranslateX(100);
pointLightFront.setTranslateY(100);
pointLightFront.setTranslateZ(-300);
pointLightFront.setRotate(90);
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);
previewPane.getChildren().add(pointLightFront);
previewPane.getChildren().add(pointLightSky);
previewPane.getChildren().add(pointLightGround);
StackPane.setAlignment(pointLightFront, Pos.CENTER);
StackPane.setAlignment(pointLightSky, Pos.CENTER);
StackPane.setAlignment(pointLightGround, Pos.CENTER);
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);
StackPane.setAlignment(myBox, Pos.CENTER);
myBox.setTranslateZ(-150);
rotateAroundYAxis(myBox).play();
previewPane.getChildren().add(myBox);
}
public StackPane getPreviewPane()
{
return previewPane;
}
private RotateTransition rotateAroundYAxis(Node node)
{
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;
}
private int calcBoxSize(int height, int width)
{
if (height <= width) {
// height is smallest dimension
return (int) (height / 2);
} else {
// width is smallest dimension
return (int) (width / 2);
}
}
}

View File

@ -1,3 +1,3 @@
main/java/com/ThreeDtest/application/Main$1.class main/java/com/ThreeDtest/application/Texture3DPreview.class
main/java/com/ThreeDtest/application/Main.class main/java/com/ThreeDtest/application/Main.class
main/java/com/ThreeDtest/application/JavaFX11Main.class main/java/com/ThreeDtest/application/JavaFX11Main.class