javaFX学习之javaFX 3d圆柱体程序demo(转载)

本文档展示了一个使用JavaFX实现的3D圆柱体演示程序,包含两个子场景,分别展示了开启和关闭多重采样抗锯齿(MSAA)的效果。通过滑块组件控制圆柱体的旋转,同时讨论了如何在JavaFX中设置光源、相机和3D场景。
摘要由CSDN通过智能技术生成
原文链接第二篇 使用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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值