diablotheboss

TRY1

Jan 27th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.88 KB | None | 0 0
  1. public class GraphingData extends Application {
  2.  
  3.     private double mousePosX, mousePosY;
  4.     private double mouseOldX, mouseOldY;
  5.     private final Rotate rotateX = new Rotate(150, Rotate.X_AXIS);
  6.     private final Rotate rotateY = new Rotate(120, Rotate.Y_AXIS);
  7.     private static Random rnd = new Random();
  8.     private int graphSize = 10;
  9.  
  10.     @Override
  11.     public void start(Stage primaryStage) throws Exception {
  12.        
  13.  
  14.         // 3D
  15.          // create axis walls
  16.         Group grid = createGrid(graphSize);        
  17.         // initial cube rotation
  18.         grid.getTransforms().addAll(rotateX, rotateY);
  19.  
  20.         /*      CUBO
  21.         Box box = new Box(5, 5, 5);
  22.         box.setMaterial(new PhongMaterial(Color.GREENYELLOW));*/
  23.    
  24.         PerspectiveCamera camera = new PerspectiveCamera(true);
  25.         camera.getTransforms().addAll (rotateX, rotateY, new Translate(0, 0, -20));
  26.    
  27.         Sphere sfera = new Sphere();
  28.         sfera.setRadius(1);
  29.        
  30.         Group root3D = new Group(camera, grid);
  31.    
  32.         SubScene subScene = new SubScene(root3D, 600, 600, true, SceneAntialiasing.BALANCED);
  33.         subScene.setFill(Color.AQUAMARINE);
  34.         subScene.setCamera(camera);
  35.    
  36.         // 2D
  37.         BorderPane pane = new BorderPane();
  38.         pane.setCenter(subScene);
  39.         Button button = new Button("Reset");
  40.         button.setOnAction(e->{
  41.             rotateX.setAngle(-20);
  42.             rotateY.setAngle(-20);
  43.         });
  44.  
  45.         ToolBar toolBar = new ToolBar(button);
  46.         toolBar.setOrientation(Orientation.VERTICAL);
  47.         pane.setRight(toolBar);
  48.         pane.setPrefSize(600,600);
  49.    
  50.         Scene scene = new Scene(pane);
  51.    
  52.         scene.setOnMousePressed(me -> {
  53.             mouseOldX = me.getSceneX();
  54.             mouseOldY = me.getSceneY();
  55.         });
  56.         scene.setOnMouseDragged(me -> {
  57.             mousePosX = me.getSceneX();
  58.             mousePosY = me.getSceneY();
  59.             rotateX.setAngle(rotateX.getAngle() - (mousePosY - mouseOldY));
  60.             rotateY.setAngle(rotateY.getAngle() + (mousePosX - mouseOldX));
  61.             mouseOldX = mousePosX;
  62.             mouseOldY = mousePosY;
  63.  
  64.         });
  65.    
  66.         primaryStage.setScene(scene);
  67.         primaryStage.setTitle("3D SubScene");
  68.         primaryStage.show();
  69.     }
  70.    
  71.    
  72.     /**
  73.      * Axis wall
  74.      */
  75.     public static class Axis extends Pane {
  76.  
  77.         Rectangle wall;
  78.  
  79.         public Axis(double size) {
  80.  
  81.             // wall
  82.             // first the wall, then the lines => overlapping of lines over walls
  83.             // works
  84.             wall = new Rectangle(size, size);
  85.             getChildren().add(wall);
  86.  
  87.             // grid
  88.             double zTranslate = 0;
  89.             double lineWidth = 1.0;
  90.             Color gridColor = Color.RED;
  91.  
  92.             for (int y = 0; y <= size; y += size / 10) {
  93.  
  94.                 Line line = new Line(0, 0, size, 0);
  95.                 line.setStroke(gridColor);
  96.                 line.setFill(gridColor);
  97.                 line.setTranslateY(y);
  98.                 line.setTranslateZ(zTranslate);
  99.                 line.setStrokeWidth(lineWidth);
  100.  
  101.                 getChildren().addAll(line);
  102.  
  103.             }
  104.  
  105.             for (int x = 0; x <= size; x += size / 10) {
  106.  
  107.                 Line line = new Line(0, 0, 0, size);
  108.                 line.setStroke(gridColor);
  109.                 line.setFill(gridColor);
  110.                 line.setTranslateX(x);
  111.                 line.setTranslateZ(zTranslate);
  112.                 line.setStrokeWidth(lineWidth);
  113.  
  114.                 getChildren().addAll(line);
  115.  
  116.             }
  117.  
  118.         }
  119.  
  120.         public void setFill(Paint paint) {
  121.             wall.setFill(paint);
  122.         }
  123.  
  124.     }
  125.  
  126.     public void makeZoomable(StackPane control) {
  127.  
  128.         final double MAX_SCALE = 20.0;
  129.         final double MIN_SCALE = 0.1;
  130.  
  131.         control.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
  132.  
  133.             @Override
  134.             public void handle(ScrollEvent event) {
  135.  
  136.                 double delta = 1.2;
  137.  
  138.                 double scale = control.getScaleX();
  139.  
  140.                 if (event.getDeltaY() < 0) {
  141.                     scale /= delta;
  142.                 } else {
  143.                     scale *= delta;
  144.                 }
  145.  
  146.                 scale = clamp(scale, MIN_SCALE, MAX_SCALE);
  147.  
  148.                 control.setScaleX(scale);
  149.                 control.setScaleY(scale);
  150.  
  151.                 event.consume();
  152.  
  153.             }
  154.  
  155.         });
  156.  
  157.     }
  158.    
  159.  
  160.     /**
  161.      * Create axis walls
  162.      *
  163.      * @param size
  164.      * @return
  165.      */
  166.     private Group createGrid(int size) {
  167.  
  168.         Group cube = new Group();
  169.  
  170.         // size of the cube
  171.         Color color = Color.LIGHTGRAY;
  172.  
  173.         List<Axis> cubeFaces = new ArrayList<>();
  174.         Axis r;
  175.  
  176.         // back face
  177.        // System.out.println("Problema");
  178.         r = new Axis(size);
  179.         r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.5 * 1), 1.0));
  180.         r.setTranslateX(-0.5 * size);
  181.         r.setTranslateY(-0.5 * size);
  182.         r.setTranslateZ(0.5 * size);
  183.        
  184.         cubeFaces.add(r);
  185.  
  186.         // bottom face
  187.         r = new Axis(size);
  188.        
  189.         r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.4 * 1), 1.0));
  190.         r.setTranslateX(-0.5 * size);
  191.         r.setTranslateY(0);
  192.         r.setRotationAxis(Rotate.X_AXIS);
  193.         r.setRotate(90);
  194.  
  195.         cubeFaces.add(r);
  196.  
  197.         // right face
  198.         r = new Axis(size);
  199.         r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.3 * 1), 1.0));
  200.         r.setTranslateX(-1 * size);
  201.         r.setTranslateY(-0.5 * size);
  202.         r.setRotationAxis(Rotate.Y_AXIS);
  203.         r.setRotate(90);
  204.  
  205.         // cubeFaces.add( r);
  206.  
  207.         // left face
  208.         r = new Axis(size);
  209.         r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.2 * 1), 1.0));
  210.         r.setTranslateX(0);
  211.         r.setTranslateY(-0.5 * size);
  212.         r.setRotationAxis(Rotate.Y_AXIS);
  213.         r.setRotate(90);
  214.  
  215.         cubeFaces.add(r);
  216.  
  217.         // top face
  218.         r = new Axis(size);
  219.         r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.1 * 1), 1.0));
  220.         r.setTranslateX(-0.5 * size);
  221.         r.setTranslateY(-1 * size);
  222.         r.setRotationAxis(Rotate.X_AXIS);
  223.         r.setRotate(90);
  224.  
  225.         // cubeFaces.add( r);
  226.  
  227.         // front face
  228.         r = new Axis(size);
  229.         r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.1 * 1), 1.0));
  230.         r.setTranslateX(-0.5 * size);
  231.         r.setTranslateY(-0.5 * size);
  232.         r.setTranslateZ(-0.5 * size);
  233.  
  234.         // cubeFaces.add( r);
  235.  
  236.         cube.getChildren().addAll(cubeFaces);
  237.        
  238.         double gridSizeHalf = size / 2;
  239.         double labelOffset = 30 ;
  240.         double labelPos = gridSizeHalf - labelOffset ;
  241.  
  242.         for (double coord = -gridSizeHalf ; coord < gridSizeHalf ; coord+=50) {
  243.             Text xLabel = new Text(coord, labelPos, String.format("%.0f", coord));
  244.             xLabel.setTranslateZ(labelPos);
  245.             xLabel.setScaleX(-1);
  246.             Text yLabel = new Text(labelPos, coord, String.format("%.0f", coord));
  247.             yLabel.setTranslateZ(labelPos);
  248.             yLabel.setScaleX(-1);
  249.             Text zLabel = new Text(labelPos, labelPos, String.format("%.0f", coord));
  250.             zLabel.setTranslateZ(coord);
  251.             cube.getChildren().addAll(xLabel, yLabel, zLabel);
  252.             zLabel.setScaleX(-1);
  253.         }
  254.        
  255.         return cube;
  256.     }
  257.  
  258.  
  259.    
  260.    
  261.    
  262.     public static double normalizeValue(double value, double min, double max, double newMin, double newMax) {
  263.  
  264.         return (value - min) * (newMax - newMin) / (max - min) + newMin;
  265.  
  266.     }
  267.  
  268.     public static double clamp(double value, double min, double max) {
  269.  
  270.         if (Double.compare(value, min) < 0)
  271.             return min;
  272.  
  273.         if (Double.compare(value, max) > 0)
  274.             return max;
  275.  
  276.         return value;
  277.     }
  278.  
  279.     public static Color randomColor() {
  280.         return Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
  281.     }
  282.    
  283.     public static void main(String[] args) {
  284.         launch(args);
  285.     }
  286.  
  287. }
Advertisement
Add Comment
Please, Sign In to add comment