Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GraphingData extends Application {
- private double mousePosX, mousePosY;
- private double mouseOldX, mouseOldY;
- private final Rotate rotateX = new Rotate(150, Rotate.X_AXIS);
- private final Rotate rotateY = new Rotate(120, Rotate.Y_AXIS);
- private static Random rnd = new Random();
- private int graphSize = 10;
- @Override
- public void start(Stage primaryStage) throws Exception {
- // 3D
- // create axis walls
- Group grid = createGrid(graphSize);
- // initial cube rotation
- grid.getTransforms().addAll(rotateX, rotateY);
- /* CUBO
- Box box = new Box(5, 5, 5);
- box.setMaterial(new PhongMaterial(Color.GREENYELLOW));*/
- PerspectiveCamera camera = new PerspectiveCamera(true);
- camera.getTransforms().addAll (rotateX, rotateY, new Translate(0, 0, -20));
- Sphere sfera = new Sphere();
- sfera.setRadius(1);
- Group root3D = new Group(camera, grid);
- SubScene subScene = new SubScene(root3D, 600, 600, true, SceneAntialiasing.BALANCED);
- subScene.setFill(Color.AQUAMARINE);
- subScene.setCamera(camera);
- // 2D
- BorderPane pane = new BorderPane();
- pane.setCenter(subScene);
- Button button = new Button("Reset");
- button.setOnAction(e->{
- rotateX.setAngle(-20);
- rotateY.setAngle(-20);
- });
- ToolBar toolBar = new ToolBar(button);
- toolBar.setOrientation(Orientation.VERTICAL);
- pane.setRight(toolBar);
- pane.setPrefSize(600,600);
- Scene scene = new Scene(pane);
- scene.setOnMousePressed(me -> {
- mouseOldX = me.getSceneX();
- mouseOldY = me.getSceneY();
- });
- scene.setOnMouseDragged(me -> {
- mousePosX = me.getSceneX();
- mousePosY = me.getSceneY();
- rotateX.setAngle(rotateX.getAngle() - (mousePosY - mouseOldY));
- rotateY.setAngle(rotateY.getAngle() + (mousePosX - mouseOldX));
- mouseOldX = mousePosX;
- mouseOldY = mousePosY;
- });
- primaryStage.setScene(scene);
- primaryStage.setTitle("3D SubScene");
- primaryStage.show();
- }
- /**
- * Axis wall
- */
- public static class Axis extends Pane {
- Rectangle wall;
- public Axis(double size) {
- // wall
- // first the wall, then the lines => overlapping of lines over walls
- // works
- wall = new Rectangle(size, size);
- getChildren().add(wall);
- // grid
- double zTranslate = 0;
- double lineWidth = 1.0;
- Color gridColor = Color.RED;
- for (int y = 0; y <= size; y += size / 10) {
- Line line = new Line(0, 0, size, 0);
- line.setStroke(gridColor);
- line.setFill(gridColor);
- line.setTranslateY(y);
- line.setTranslateZ(zTranslate);
- line.setStrokeWidth(lineWidth);
- getChildren().addAll(line);
- }
- for (int x = 0; x <= size; x += size / 10) {
- Line line = new Line(0, 0, 0, size);
- line.setStroke(gridColor);
- line.setFill(gridColor);
- line.setTranslateX(x);
- line.setTranslateZ(zTranslate);
- line.setStrokeWidth(lineWidth);
- getChildren().addAll(line);
- }
- }
- public void setFill(Paint paint) {
- wall.setFill(paint);
- }
- }
- public void makeZoomable(StackPane control) {
- final double MAX_SCALE = 20.0;
- final double MIN_SCALE = 0.1;
- control.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
- @Override
- public void handle(ScrollEvent event) {
- double delta = 1.2;
- double scale = control.getScaleX();
- if (event.getDeltaY() < 0) {
- scale /= delta;
- } else {
- scale *= delta;
- }
- scale = clamp(scale, MIN_SCALE, MAX_SCALE);
- control.setScaleX(scale);
- control.setScaleY(scale);
- event.consume();
- }
- });
- }
- /**
- * Create axis walls
- *
- * @param size
- * @return
- */
- private Group createGrid(int size) {
- Group cube = new Group();
- // size of the cube
- Color color = Color.LIGHTGRAY;
- List<Axis> cubeFaces = new ArrayList<>();
- Axis r;
- // back face
- // System.out.println("Problema");
- r = new Axis(size);
- r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.5 * 1), 1.0));
- r.setTranslateX(-0.5 * size);
- r.setTranslateY(-0.5 * size);
- r.setTranslateZ(0.5 * size);
- cubeFaces.add(r);
- // bottom face
- r = new Axis(size);
- r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.4 * 1), 1.0));
- r.setTranslateX(-0.5 * size);
- r.setTranslateY(0);
- r.setRotationAxis(Rotate.X_AXIS);
- r.setRotate(90);
- cubeFaces.add(r);
- // right face
- r = new Axis(size);
- r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.3 * 1), 1.0));
- r.setTranslateX(-1 * size);
- r.setTranslateY(-0.5 * size);
- r.setRotationAxis(Rotate.Y_AXIS);
- r.setRotate(90);
- // cubeFaces.add( r);
- // left face
- r = new Axis(size);
- r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.2 * 1), 1.0));
- r.setTranslateX(0);
- r.setTranslateY(-0.5 * size);
- r.setRotationAxis(Rotate.Y_AXIS);
- r.setRotate(90);
- cubeFaces.add(r);
- // top face
- r = new Axis(size);
- r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.1 * 1), 1.0));
- r.setTranslateX(-0.5 * size);
- r.setTranslateY(-1 * size);
- r.setRotationAxis(Rotate.X_AXIS);
- r.setRotate(90);
- // cubeFaces.add( r);
- // front face
- r = new Axis(size);
- r.setFill(color.deriveColor(0.0, 1.0, (1 - 0.1 * 1), 1.0));
- r.setTranslateX(-0.5 * size);
- r.setTranslateY(-0.5 * size);
- r.setTranslateZ(-0.5 * size);
- // cubeFaces.add( r);
- cube.getChildren().addAll(cubeFaces);
- double gridSizeHalf = size / 2;
- double labelOffset = 30 ;
- double labelPos = gridSizeHalf - labelOffset ;
- for (double coord = -gridSizeHalf ; coord < gridSizeHalf ; coord+=50) {
- Text xLabel = new Text(coord, labelPos, String.format("%.0f", coord));
- xLabel.setTranslateZ(labelPos);
- xLabel.setScaleX(-1);
- Text yLabel = new Text(labelPos, coord, String.format("%.0f", coord));
- yLabel.setTranslateZ(labelPos);
- yLabel.setScaleX(-1);
- Text zLabel = new Text(labelPos, labelPos, String.format("%.0f", coord));
- zLabel.setTranslateZ(coord);
- cube.getChildren().addAll(xLabel, yLabel, zLabel);
- zLabel.setScaleX(-1);
- }
- return cube;
- }
- public static double normalizeValue(double value, double min, double max, double newMin, double newMax) {
- return (value - min) * (newMax - newMin) / (max - min) + newMin;
- }
- public static double clamp(double value, double min, double max) {
- if (Double.compare(value, min) < 0)
- return min;
- if (Double.compare(value, max) > 0)
- return max;
- return value;
- }
- public static Color randomColor() {
- return Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment