/* * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ import javafx.animation.RotateTransition; import javafx.application.Application; import javafx.builders.GroupBuilder; import javafx.builders.PolygonBuilder; import javafx.builders.SceneBuilder; import javafx.geometry.Point3D; import javafx.scene.DepthTest; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; import javafx.scene.transform.Rotate; import javafx.stage.Stage; import javafx.util.Duration; /** * * @author Alexander Kouznetsov */ public class ClippingTest extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Group root = GroupBuilder.create() .children(new Triangle(new Point3D(0, 0, 0), new Point3D(60, 0, 0), new Point3D(30, 50, 0))) .translateX(150) .translateY(150) .translateZ(0) // .rotationAxis(Rotate.Y_AXIS) // .rotate(30) .build(); // Timeline animation = TimelineBuilder.create() // .cycleCount(Timeline.INDEFINITE) // .keyFrames(new KeyFrame(Duration.valueOf(3500), new KeyValue(root.translateZProperty(), 30)), // new KeyFrame(Duration.valueOf(7000), new KeyValue(root.translateZProperty(), 0))) // .build(); // animation.play(); root.setRotationAxis(Rotate.Y_AXIS); RotateTransition rt = new RotateTransition(Duration.valueOf(5000), root); rt.setByAngle(360); rt.play(); root.setDepthTest(DepthTest.ENABLE); Scene scene = SceneBuilder.create() .root(root) .camera(new PerspectiveCamera()) .build(); primaryStage.setScene(scene); primaryStage.setVisible(true); } } /** * * @author Alexander Kouznetsov */ class Triangle extends Group { private Point3D vertex1; private Point3D vertex2; private Point3D vertex3; /** * Creates triangle. Z is not used yet * @param vertex1 * @param vertex2 * @param vertex3 */ public Triangle(Point3D vertex1, Point3D vertex2, Point3D vertex3) { this.vertex1 = vertex1; this.vertex2 = vertex2; this.vertex3 = vertex3; Point3D v1 = new Point3D( vertex2.getX() - vertex1.getX(), vertex2.getY() - vertex1.getY(), vertex2.getZ() - vertex1.getZ()); Point3D v2 = new Point3D( vertex3.getX() - vertex1.getX(), vertex3.getY() - vertex1.getY(), vertex3.getZ() - vertex1.getZ()); double x1, y1, x2, y2; x1 = v1.getX(); y1 = v1.getY(); x2 = v2.getX(); y2 = v2.getY(); Polygon clipping = PolygonBuilder.create() .points(0d, 0d, x1, y1, x2, y2) .build(); Rectangle node = new Rectangle(Math.max(x1, x2), Math.max(y1, y2), Color.RED); node.setClip(clipping); getChildren().setAll(node); } }