Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.application.Application;
  4. import javafx.beans.property.DoubleProperty;
  5. import javafx.beans.property.SimpleDoubleProperty;
  6. import javafx.scene.Group;
  7. import javafx.scene.PerspectiveCamera;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.input.ScrollEvent;
  12. import javafx.scene.layout.GridPane;
  13. import javafx.scene.paint.Color;
  14. import javafx.scene.paint.Material;
  15. import javafx.scene.paint.PhongMaterial;
  16. import javafx.scene.shape.Box;
  17. import javafx.scene.transform.Rotate;
  18. import javafx.stage.Stage;
  19.  
  20.  
  21. public class Main extends Application {
  22.  
  23. //Tracks drag starting point for x and y
  24. private double anchorX, anchorY;
  25. //Keep track of current angle for x and y
  26. private double anchorAngleX = 0;
  27. private double anchorAngleY = 0;
  28. //We will update these after drag. Using JavaFX property to bind with object
  29. private final DoubleProperty angleX = new SimpleDoubleProperty(0);
  30. private final DoubleProperty angleY = new SimpleDoubleProperty(0);
  31.  
  32. @Override
  33. public void start(Stage primaryStage) throws Exception {
  34. // TODO Auto-generated method stub
  35. Box box2 = new Box();
  36.  
  37.  
  38.  
  39. final PhongMaterial blueMaterial = new PhongMaterial();
  40. blueMaterial.setDiffuseColor(Color.BLUE);
  41. blueMaterial.setSpecularColor(Color.LIGHTBLUE);
  42.  
  43.  
  44.  
  45.  
  46. //Setting properties for second box
  47. box2.setTranslateX(200);
  48. box2.setTranslateY(300);
  49. box2.setTranslateZ(100);
  50. box2.setHeight(200);
  51. box2.setWidth(500);
  52. box2.setDepth(300);
  53.  
  54.  
  55.  
  56. box2.setMaterial(blueMaterial);
  57.  
  58. Button button = new Button();
  59. button.setTranslateX(1000);
  60. button.setTranslateY(300);
  61. button.setTranslateZ(100);
  62.  
  63. button.setText("Wojtek");
  64.  
  65.  
  66. Label label = new Label();
  67. label.setTranslateX(200);
  68. label.setTranslateY(300);
  69. label.setTranslateZ(-45);
  70.  
  71.  
  72.  
  73.  
  74. //Setting the perspective camera
  75. PerspectiveCamera camera = new PerspectiveCamera();
  76. camera.setTranslateX(0);
  77. camera.setTranslateY(0);
  78. camera.setTranslateZ(0);
  79.  
  80. //Configuring Group, Scene and Stage
  81. Group root = new Group();
  82. root.getChildren().addAll(box2,label,button);
  83. label.setText("Wojtek");
  84. Scene scene = new Scene(root,450,350,Color.LIMEGREEN);
  85. scene.setCamera(camera);
  86.  
  87. primaryStage.setScene(scene);
  88. primaryStage.setTitle("Box Example");
  89. primaryStage.show();
  90.  
  91. initMouseControl(root,scene);
  92.  
  93.  
  94. primaryStage.addEventHandler(ScrollEvent.SCROLL, event -> {
  95. //Get how much scroll was done in Y axis.
  96. double delta = event.getDeltaY();
  97. //Add it to the Z-axis location.
  98. root.translateZProperty().set(root.getTranslateZ() + delta);
  99. });
  100. }
  101.  
  102.  
  103.  
  104. private void initMouseControl(Group group, Scene scene) {
  105. Rotate xRotate;
  106. Rotate yRotate;
  107. group.getTransforms().addAll(
  108. xRotate = new Rotate(0, Rotate.X_AXIS),
  109. yRotate = new Rotate(0, Rotate.Y_AXIS)
  110. );
  111. xRotate.angleProperty().bind(angleX);
  112. yRotate.angleProperty().bind(angleY);
  113.  
  114. scene.setOnMousePressed(event -> {
  115. anchorX = event.getSceneX();
  116. anchorY = event.getSceneY();
  117. anchorAngleX = angleX.get();
  118. anchorAngleY = angleY.get();
  119. });
  120.  
  121. scene.setOnMouseDragged(event -> {
  122. angleX.set(anchorAngleX - (anchorY - event.getSceneY()));
  123. angleY.set(anchorAngleY + anchorX - event.getSceneX());
  124. });
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. public static void main(String[] args) {
  132. launch(args);
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement