原文链接第二篇 使用JavaFX图形 | JavaFX中文资料 package msaa; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.application.ConditionalFeature; import javafx.application.Platform; import javafx.geometry.Point3D; import javafx.scene.AmbientLight; import javafx.scene.Camera; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Parent; import javafx.scene.PerspectiveCamera; import javafx.scene.PointLight; import javafx.scene.Scene; import javafx.scene.SceneAntialiasing; import javafx.scene.SubScene; import javafx.scene.control.Slider; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.Cylinder; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; public class MSAAApp extends Application { @Override public void start(Stage stage) {//启动舞台 if (!Platform.isSupported(ConditionalFeature.SCENE3D)) {//判断操作系统是否支持3d场景 throw new RuntimeException("*** ERROR: common conditional SCENE3D is not supported"); } stage.setTitle("JavaFX 3d圆柱体 demo"); Group root = new Group();//创建一个节点分组 Scene scene = new Scene(root, 1000, 800); scene.setFill(Color.color(0.2, 0.2, 0.2, 1.0)); HBox hbox = new HBox();//创建一个水平盒子布局器对象 hbox.setLayoutX(75);//设置布局器的x轴坐标 hbox.setLayoutY(200);//设置布局器的y轴坐标 PhongMaterial phongMaterial = new PhongMaterial(Color.color(1.0, 0.7, 0.8));//创建一个表面材料对象 Cylinder cylinder1 = new Cylinder(100, 200);//创建个圆柱体对象 cylinder1.setMaterial(phongMaterial);//给圆柱体包个皮儿 SubScene noMsaa = createSubScene("MSAA = false", cylinder1, Color.TRANSPARENT, new PerspectiveCamera(), false);//创建一个子场景 hbox.getChildren().add(noMsaa);//将盒子布局器上添加一个子场景对象(本例中为无Msaa锯齿状显示效果的子3d场景) Cylinder cylinder2 = new Cylinder(100, 200);//创建个圆柱体2 cylinder2.setMaterial(phongMaterial);//将圆柱体2包个外皮儿 SubScene msaa = createSubScene("MSAA = true", cylinder2, Color.TRANSPARENT, new PerspectiveCamera(), true);//创建一个带有锯齿状显示特效的3d子场景,并将刚才建立好的cylider2圆柱体对象加入该子场景中 hbox.getChildren().add(msaa);//在盒子布局器上再加入一个3d自场景对象msaa Slider slider = new Slider(0, 360, 0);//创建一个滑块组件对象 slider.setBlockIncrement(1);//设置滑块组件的滑动增量参数 slider.setTranslateX(425);//设置滑块组件的转移x轴坐标值 slider.setTranslateY(625);//设置滑块组件的转移y轴坐标值 cylinder1.rotateProperty().bind(slider.valueProperty());//将圆柱体1的旋转特效属性绑定到slider滑块组件对应的滑动交互取值,并设置圆柱体的旋转特效 cylinder2.rotateProperty().bind(slider.valueProperty());//将圆柱体2的旋转特效属性绑定到slider滑块组件对应的滑动交互取值,并设置圆柱体的旋转特效 root.getChildren().addAll(hbox, slider);//将初始化根节点分组对象上添加水平盒子布局器对hbox和slider滑块组件对象 stage.setScene(scene);//舞台上装载整体场景对象scene stage.show();//展现舞台效果 } private static Parent setTitle(String str) { final VBox vbox = new VBox();//创建一个垂直盒子布局器 final Text text = new Text(str);//创建一个文本组件 text.setFont(Font.font("Times New Roman", 24));//设置组件字体 text.setFill(Color.WHEAT);//设置字的颜色 vbox.getChildren().add(text);//将文本控件添加到垂直布局器组件中 return vbox; } private static SubScene createSubScene(String title, Node node, Paint fillPaint, Camera camera, boolean msaa) { Group root = new Group();//创建一个节点分组 PointLight light = new PointLight(Color.WHITE);//创建一个灯光特效 light.setTranslateX(50); light.setTranslateY(-300); light.setTranslateZ(-400); PointLight light2 = new PointLight(Color.color(0.6, 0.3, 0.4));//再创建也给灯光特效 light2.setTranslateX(400); light2.setTranslateY(0); light2.setTranslateZ(-400); AmbientLight ambientLight = new AmbientLight(Color.color(0.2, 0.2, 0.2));//创建一个曝光特效 node.setRotationAxis(new Point3D(2, 1, 0).normalize());//将传进来的节点对象的旋转轴设置为一个点 node.setTranslateX(180); node.setTranslateY(180); root.getChildren().addAll(setTitle(title), ambientLight, light, light2, node); SubScene subScene = new SubScene(root, 500, 400, true, msaa ? SceneAntialiasing.BALANCED : SceneAntialiasing.DISABLED);//再创建一个子场景并根据设置参数设置3d效果锯齿状显示效果 subScene.setFill(fillPaint);//将子场景场景上添置绘图内容 subScene.setCamera(camera);//子场景上挂载镜头对象 return subScene;//返回所创建的子场景对象 } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }