Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.image.Image;
- import javafx.scene.shape.TriangleMesh;
- import javafx.scene.shape.MeshView;
- import javafx.scene.shape.DrawMode;
- import javafx.scene.paint.PhongMaterial;
- import javafx.scene.PerspectiveCamera;
- import javafx.scene.transform.Rotate;
- import javafx.scene.paint.Color;
- import javafx.scene.AmbientLight;
- import javafx.event.EventHandler;
- import javafx.scene.input.KeyEvent;
- public class Pyramid2 extends Application{
- MeshView pyramid;
- public void start(Stage primaryStage) {
- Group gp = new Group();
- TriangleMesh pyramidMesh = new TriangleMesh();
- pyramidMesh.getTexCoords().addAll(
- 0.3f, 0f, // point 0
- 0.2f, 0.55f,// point 1
- 0.6f, 0.55f,// point 2
- 0.7f, 0f, // point 3
- 0.7f, 0.3f, // point 4
- 0.8f, 0.3f, // point 5
- 0.8f, 0f, // point 6
- 0, 0 //point 7
- );
- Float h = 150f, s = 300f;
- pyramidMesh.getPoints().addAll(
- 0, 0, 0, // vertex 0 : Top
- 0, h, -s/2,// vertex 1 : Front
- -s/2, h, 0, // vertex 2 : Left
- s/2, h, 0, // vertex 3 : Right
- 0, h, s/2 // vertex 4 : Back
- );
- pyramidMesh.getFaces().addAll(
- 0,0, 2,1, 1,2,// Front-Left FACE
- 0,0, 1,1, 3,2,
- 0,0, 3,1, 4,2,
- 0,0, 4,1, 2,2,
- 2,3, 3,4, 4,5,
- 2,3, 1,5, 3,6
- );
- pyramid = new MeshView(pyramidMesh);
- pyramid.setDrawMode(DrawMode.FILL);
- PhongMaterial ph = new PhongMaterial();
- //ph.setDiffuseColor(Color.YELLOW);
- ph.setDiffuseMap(new Image("file:C:\\Users\\LedInfo\\Pictures\\Textures\\balloon.jpg"));
- pyramid.setMaterial(ph);
- gp.getChildren().add(pyramid);
- gp.getChildren().add(new AmbientLight());
- Scene scn = new Scene(gp, 1200, 900);
- scn.setFill(Color.SKYBLUE);
- PerspectiveCamera cam = new PerspectiveCamera(true);
- cam.setTranslateZ(-1100);
- cam.setNearClip(1);
- cam.setFarClip(12800);
- scn.setCamera(cam);
- scn.addEventHandler(KeyEvent.KEY_PRESSED, new RotationHandler());
- primaryStage.setTitle("The Pyramid");
- primaryStage.setScene(scn);
- primaryStage.show();
- }
- class RotationHandler implements EventHandler<KeyEvent>{
- public void handle(KeyEvent e) {
- switch(e.getCode()) {
- case X : pyramid.getTransforms().add(new Rotate(10, 0, 75, 0, Rotate.X_AXIS)); break;
- case C : pyramid.getTransforms().add(new Rotate(-10, 0, 75, 0, Rotate.X_AXIS)); break;
- case Y : pyramid.getTransforms().add(new Rotate(10, 0, 75, 0, Rotate.Y_AXIS)); break;
- case U : pyramid.getTransforms().add(new Rotate(-10, 0, 75, 0, Rotate.Y_AXIS)); break;
- case Z : pyramid.getTransforms().add(new Rotate(10, 0, 75, 0, Rotate.Z_AXIS)); break;
- case E : pyramid.getTransforms().add(new Rotate(-10, 0, 75, 0, Rotate.Z_AXIS)); break;
- }
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement