Advertisement
Guest User

Untitled

a guest
May 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.stage.Stage;
  3. import javafx.scene.Group;
  4. import javafx.scene.Scene;
  5. import javafx.scene.image.Image;
  6. import javafx.scene.shape.TriangleMesh;
  7. import javafx.scene.shape.MeshView;
  8. import javafx.scene.shape.DrawMode;
  9. import javafx.scene.paint.PhongMaterial;
  10. import javafx.scene.PerspectiveCamera;
  11. import javafx.scene.transform.Rotate;
  12. import javafx.scene.paint.Color;
  13. import javafx.scene.AmbientLight;
  14. import javafx.event.EventHandler;
  15. import javafx.scene.input.KeyEvent;
  16.  
  17.  
  18.  
  19. public class Pyramid2 extends Application{
  20.  
  21. MeshView pyramid;
  22.  
  23. public void start(Stage primaryStage) {
  24.  
  25. Group gp = new Group();
  26.  
  27. TriangleMesh pyramidMesh = new TriangleMesh();
  28.  
  29. pyramidMesh.getTexCoords().addAll(
  30. 0.3f, 0f, // point 0
  31. 0.2f, 0.55f,// point 1
  32. 0.6f, 0.55f,// point 2
  33. 0.7f, 0f, // point 3
  34. 0.7f, 0.3f, // point 4
  35. 0.8f, 0.3f, // point 5
  36. 0.8f, 0f, // point 6
  37. 0, 0 //point 7
  38.  
  39. );
  40.  
  41. Float h = 150f, s = 300f;
  42.  
  43. pyramidMesh.getPoints().addAll(
  44. 0, 0, 0, // vertex 0 : Top
  45. 0, h, -s/2,// vertex 1 : Front
  46. -s/2, h, 0, // vertex 2 : Left
  47. s/2, h, 0, // vertex 3 : Right
  48. 0, h, s/2 // vertex 4 : Back
  49.  
  50. );
  51.  
  52. pyramidMesh.getFaces().addAll(
  53. 0,0, 2,1, 1,2,// Front-Left FACE
  54. 0,0, 1,1, 3,2,
  55. 0,0, 3,1, 4,2,
  56. 0,0, 4,1, 2,2,
  57. 2,3, 3,4, 4,5,
  58. 2,3, 1,5, 3,6
  59. );
  60.  
  61. pyramid = new MeshView(pyramidMesh);
  62. pyramid.setDrawMode(DrawMode.FILL);
  63. PhongMaterial ph = new PhongMaterial();
  64. //ph.setDiffuseColor(Color.YELLOW);
  65. ph.setDiffuseMap(new Image("file:C:\\Users\\LedInfo\\Pictures\\Textures\\balloon.jpg"));
  66. pyramid.setMaterial(ph);
  67. gp.getChildren().add(pyramid);
  68. gp.getChildren().add(new AmbientLight());
  69.  
  70.  
  71.  
  72. Scene scn = new Scene(gp, 1200, 900);
  73. scn.setFill(Color.SKYBLUE);
  74.  
  75. PerspectiveCamera cam = new PerspectiveCamera(true);
  76. cam.setTranslateZ(-1100);
  77. cam.setNearClip(1);
  78. cam.setFarClip(12800);
  79. scn.setCamera(cam);
  80.  
  81. scn.addEventHandler(KeyEvent.KEY_PRESSED, new RotationHandler());
  82.  
  83. primaryStage.setTitle("The Pyramid");
  84. primaryStage.setScene(scn);
  85. primaryStage.show();
  86.  
  87. }
  88.  
  89. class RotationHandler implements EventHandler<KeyEvent>{
  90.  
  91. public void handle(KeyEvent e) {
  92.  
  93. switch(e.getCode()) {
  94. case X : pyramid.getTransforms().add(new Rotate(10, 0, 75, 0, Rotate.X_AXIS)); break;
  95. case C : pyramid.getTransforms().add(new Rotate(-10, 0, 75, 0, Rotate.X_AXIS)); break;
  96.  
  97. case Y : pyramid.getTransforms().add(new Rotate(10, 0, 75, 0, Rotate.Y_AXIS)); break;
  98. case U : pyramid.getTransforms().add(new Rotate(-10, 0, 75, 0, Rotate.Y_AXIS)); break;
  99.  
  100. case Z : pyramid.getTransforms().add(new Rotate(10, 0, 75, 0, Rotate.Z_AXIS)); break;
  101. case E : pyramid.getTransforms().add(new Rotate(-10, 0, 75, 0, Rotate.Z_AXIS)); break;
  102. }
  103.  
  104. }
  105. }
  106.  
  107.  
  108. public static void main(String[] args) {
  109. launch(args);
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement