diff options
Diffstat (limited to 'src/main/java/com/example')
| -rw-r--r-- | src/main/java/com/example/App.java | 78 | ||||
| -rw-r--r-- | src/main/java/com/example/BoxModel.java | 48 | ||||
| -rw-r--r-- | src/main/java/com/example/BoxRenderer.java | 64 |
3 files changed, 186 insertions, 4 deletions
diff --git a/src/main/java/com/example/App.java b/src/main/java/com/example/App.java index 0290f7b..17bf43a 100644 --- a/src/main/java/com/example/App.java +++ b/src/main/java/com/example/App.java @@ -1,23 +1,93 @@ package com.example; +import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.canvas.Canvas; import javafx.scene.Scene; +import javafx.scene.control.Button; import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class App extends Application { @Override public void start(Stage stage) { - var label = new Label("Hello JavaFX from Nix Flakes"); - var root = new StackPane(label); - root.setPadding(new Insets(24)); + var widthInput = new TextField("120"); + var heightInput = new TextField("120"); + var depthInput = new TextField("120"); + widthInput.setPrefColumnCount(4); + heightInput.setPrefColumnCount(4); + depthInput.setPrefColumnCount(4); + + var applyButton = new Button("Apply"); + var controls = new HBox( + 8, + new Label("W:"), widthInput, + new Label("H:"), heightInput, + new Label("D:"), depthInput, + applyButton + ); + controls.setAlignment(Pos.CENTER_LEFT); + controls.getStyleClass().add("controls-bar"); + widthInput.getStyleClass().add("dimension-input"); + heightInput.getStyleClass().add("dimension-input"); + depthInput.getStyleClass().add("dimension-input"); + applyButton.getStyleClass().add("apply-button"); + + var canvas = new Canvas(500, 300); + var canvasHolder = new StackPane(canvas); + canvasHolder.getStyleClass().add("canvas-holder"); + var gc = canvas.getGraphicsContext2D(); + var box = new BoxModel(120, 120, 120); + var renderer = new BoxRenderer(); + + applyButton.setOnAction(event -> { + box.setWidth(parseSize(widthInput, box.getWidth())); + box.setHeight(parseSize(heightInput, box.getHeight())); + box.setDepth(parseSize(depthInput, box.getDepth())); + }); + + var root = new BorderPane(); + root.setPadding(new Insets(16)); + root.getStyleClass().add("app-root"); + root.setTop(controls); + root.setCenter(canvasHolder); + + applyButton.fire(); var scene = new Scene(root, 640, 360); - stage.setTitle("JavaFX Projekt"); + scene.getStylesheets().add(getClass().getResource("/app.css").toExternalForm()); + stage.setTitle("Spinning Box"); stage.setScene(scene); stage.show(); + + var start = System.nanoTime(); + new AnimationTimer() { + @Override + public void handle(long now) { + double t = (now - start) / 1000000000.0; + double angleY = t; + double angleX = t * 0.6; + renderer.draw(gc, canvas.getWidth(), canvas.getHeight(), box, angleX, angleY); + } + }.start(); + } + + private static double parseSize(TextField field, double fallback) { + double value; + try { + value = Double.parseDouble(field.getText()); + } catch (NumberFormatException e) { + value = fallback; + } + value = Math.max(20, Math.min(value, 220)); + field.setText(String.format("%.0f", value)); + return value; } public static void main(String[] args) { diff --git a/src/main/java/com/example/BoxModel.java b/src/main/java/com/example/BoxModel.java new file mode 100644 index 0000000..0dd4e65 --- /dev/null +++ b/src/main/java/com/example/BoxModel.java @@ -0,0 +1,48 @@ +package com.example; + +public class BoxModel { + private double width; + private double height; + private double depth; + + private double area; + private double volume; + + public BoxModel(double width, double height, double depth) { + this.width = width; + this.height = height; + this.depth = depth; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + this.width = width; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + public double getDepth() { + return depth; + } + + public void setDepth(double depth) { + this.depth = depth; + } + + public double getVolume(){ + return this.width * this.height * this.depth; + } + + public double getArea(){ + return 2 * (this.width * this.height + this.width * this.depth + this.height * this.depth); + } +} diff --git a/src/main/java/com/example/BoxRenderer.java b/src/main/java/com/example/BoxRenderer.java new file mode 100644 index 0000000..9ec9c43 --- /dev/null +++ b/src/main/java/com/example/BoxRenderer.java @@ -0,0 +1,64 @@ +package com.example; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class BoxRenderer { + private static final int[][] EDGES = { + {0, 1}, {1, 2}, {2, 3}, {3, 0}, + {4, 5}, {5, 6}, {6, 7}, {7, 4}, + {0, 4}, {1, 5}, {2, 6}, {3, 7} + }; + + public void draw( + GraphicsContext gc, + double canvasWidth, + double canvasHeight, + BoxModel box, + double angleX, + double angleY + ) { + double hw = box.getWidth() / 2.0; + double hh = box.getHeight() / 2.0; + double hd = box.getDepth() / 2.0; + + double[][] vertices = { + {-hw, -hh, -hd}, {hw, -hh, -hd}, {hw, hh, -hd}, {-hw, hh, -hd}, + {-hw, -hh, hd}, {hw, -hh, hd}, {hw, hh, hd}, {-hw, hh, hd} + }; + + double[][] projected = new double[8][2]; + double cx = canvasWidth / 2.0; + double cy = canvasHeight / 2.0; + double cosY = Math.cos(angleY); + double sinY = Math.sin(angleY); + double cosX = Math.cos(angleX); + double sinX = Math.sin(angleX); + double scale = 1.6; + + for (int i = 0; i < vertices.length; i++) { + double x = vertices[i][0]; + double y = vertices[i][1]; + double z = vertices[i][2]; + + double x1 = x * cosY + z * sinY; + double z1 = -x * sinY + z * cosY; + double y2 = y * cosX - z1 * sinX; + double z2 = y * sinX + z1 * cosX; + + projected[i][0] = cx + x1 * scale; + projected[i][1] = cy + y2 * scale + z2 * 0.08; + } + + gc.setFill(Color.WHITE); + gc.fillRect(0, 0, canvasWidth, canvasHeight); + + gc.setStroke(Color.DARKBLUE); + gc.setLineWidth(2); + for (int[] edge : EDGES) { + int a = edge[0]; + int b = edge[1]; + gc.strokeLine(projected[a][0], projected[a][1], projected[b][0], projected[b][1]); + } + } +} |
