Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.application.Application;
- import javafx.beans.property.DoubleProperty;
- import javafx.beans.property.SimpleDoubleProperty;
- import javafx.scene.Group;
- import javafx.scene.PerspectiveCamera;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.input.ScrollEvent;
- import javafx.scene.layout.GridPane;
- import javafx.scene.paint.Color;
- import javafx.scene.paint.Material;
- import javafx.scene.paint.PhongMaterial;
- import javafx.scene.shape.Box;
- import javafx.scene.transform.Rotate;
- import javafx.stage.Stage;
- public class Main extends Application {
- //Tracks drag starting point for x and y
- private double anchorX, anchorY;
- //Keep track of current angle for x and y
- private double anchorAngleX = 0;
- private double anchorAngleY = 0;
- //We will update these after drag. Using JavaFX property to bind with object
- private final DoubleProperty angleX = new SimpleDoubleProperty(0);
- private final DoubleProperty angleY = new SimpleDoubleProperty(0);
- @Override
- public void start(Stage primaryStage) throws Exception {
- // TODO Auto-generated method stub
- Box box2 = new Box();
- final PhongMaterial blueMaterial = new PhongMaterial();
- blueMaterial.setDiffuseColor(Color.BLUE);
- blueMaterial.setSpecularColor(Color.LIGHTBLUE);
- //Setting properties for second box
- box2.setTranslateX(200);
- box2.setTranslateY(300);
- box2.setTranslateZ(100);
- box2.setHeight(200);
- box2.setWidth(500);
- box2.setDepth(300);
- box2.setMaterial(blueMaterial);
- Button button = new Button();
- button.setTranslateX(1000);
- button.setTranslateY(300);
- button.setTranslateZ(100);
- button.setText("Wojtek");
- Label label = new Label();
- label.setTranslateX(200);
- label.setTranslateY(300);
- label.setTranslateZ(-45);
- //Setting the perspective camera
- PerspectiveCamera camera = new PerspectiveCamera();
- camera.setTranslateX(0);
- camera.setTranslateY(0);
- camera.setTranslateZ(0);
- //Configuring Group, Scene and Stage
- Group root = new Group();
- root.getChildren().addAll(box2,label,button);
- label.setText("Wojtek");
- Scene scene = new Scene(root,450,350,Color.LIMEGREEN);
- scene.setCamera(camera);
- primaryStage.setScene(scene);
- primaryStage.setTitle("Box Example");
- primaryStage.show();
- initMouseControl(root,scene);
- primaryStage.addEventHandler(ScrollEvent.SCROLL, event -> {
- //Get how much scroll was done in Y axis.
- double delta = event.getDeltaY();
- //Add it to the Z-axis location.
- root.translateZProperty().set(root.getTranslateZ() + delta);
- });
- }
- private void initMouseControl(Group group, Scene scene) {
- Rotate xRotate;
- Rotate yRotate;
- group.getTransforms().addAll(
- xRotate = new Rotate(0, Rotate.X_AXIS),
- yRotate = new Rotate(0, Rotate.Y_AXIS)
- );
- xRotate.angleProperty().bind(angleX);
- yRotate.angleProperty().bind(angleY);
- scene.setOnMousePressed(event -> {
- anchorX = event.getSceneX();
- anchorY = event.getSceneY();
- anchorAngleX = angleX.get();
- anchorAngleY = angleY.get();
- });
- scene.setOnMouseDragged(event -> {
- angleX.set(anchorAngleX - (anchorY - event.getSceneY()));
- angleY.set(anchorAngleY + anchorX - event.getSceneX());
- });
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement