JavaFX - 3D 形状

在前面的章节中,我们已经了解了如何在 JavaFX 应用程序中的 XY 平面上绘制 2D 形状。除了这些 2D 形状之外,我们还可以使用 JavaFX 绘制其他几个 3D 形状。

通常,3D 形状是可以在 XYZ 平面上绘制的几何图形。它们由两个或多个维度定义,通常是 length, width and depth。JavaFX 支持的 3D 形状包括 Cylinder、Sphere 和 Box。

上面提到的每个 3D 形状都由一个类表示,所有这些类都属于 javafx.scene.shape 包。名为 Shape3D 的类是 JavaFX 中所有 3 维形状的基类。

创建 3D 形状

要创建 3-Dimensional 形状,需要

实例化相应的类

要创建一个 3-Dimensional 形状,首先你需要实例化它各自的类。例如,如果要创建一个 3D 框,则需要实例化名为 Box 的类,如下所示

Box box = new Box();

设置形状的属性

实例化类后,需要使用 setter 方法设置形状的属性。

例如,要绘制 3D 框,需要传递其 Width、Height、Depth。您可以使用各自的 setter 方法指定这些值,如下所示

//Setting the properties of the Box 
box.setWidth(200.0); 
box.setHeight(400.0);   
box.setDepth(200.0);

将 Shape 对象添加到组中

最后,需要通过将形状的对象作为构造函数的参数传递来将其添加到组中,如下所示。 

//Creating a Group object  
Group root = new Group(box);
S.No

形状和描述

1

Box

长方体是具有length (depth), width, and a height.
长方体是具有 length (depth) 、 width 和 height 的三维形状。

在 JavaFX 中,三维框由名为 Box 的类表示。此类属于 javafx.scene.shape 包。

通过实例化此类,可以在 JavaFX 中创建一个 Box 节点。

此类具有 double 数据类型的 3 个属性

width − 框的宽度

height − 框的高度

depth - 框的深度

2

Cylinder

圆柱体是一种封闭的实体,具有两个平行(大部分为圆形)的底面,由曲面连接。

它由两个参数描述,即其圆形底面的半径和圆柱体的高度。

在 JavaFX 中,圆柱体由名为 Cylinder 的类表示。此类属于 javafx.scene.shape 包。

通过实例化此类,您可以在 JavaFX 中创建一个圆柱体节点。此类具有 double 数据类型的 2 个属性

height − 圆柱体的高度

radius - 圆柱体的半径

3

Sphere

球体定义为与 3D 空间中的给定点的距离相同的点集 r。这个距离 r 是球体的半径,给定的点是球体的中心。

在 JavaFX 中,球体由名为 Sphere 的类表示。此类属于 javafx.scene.shape 包。

通过实例化此类,可以在 JavaFX 中创建一个球体节点。

此类具有名为 radius 的 double 数据类型的属性。它表示 Sphere 的半径。

3D 对象的属性

对于所有 3 维对象,可以在 JavaFX 中设置各种属性。它们在下面列出 

 

JavaFX - 创建一个 Box

长方体是三维立体形状。长方体由 6 个矩形组成,这些矩形以直角放置。使用方形面的长方体是立方体,如果面是矩形,而不是立方体,则它看起来像一个鞋盒。

长方体是具有 length (depth)、width 和 height 的三维形状。在 JavaFX 中,这种类型的三维形状被寻址为 Box;因为它可以是长方体或立方体,具体取决于形状的测量值。

在 JavaFX 中,三维框由名为 Box 的类表示。此类属于 javafx.scene.shape 包。通过实例化此类,可以在 JavaFX 中创建一个 Box 节点。 

绘制 3D 框的步骤

第 1 步:创建 Box

可以通过实例化名为 BOX 的类在 JavaFX 中创建 Box,该类属于包 javafx.scene.shape 。您可以在 Application 类的 start() 方法中实例化此类,如下所示

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the class Box 
      Box box = new Box();   
   }    
}

第 2 步:将属性设置为框

使用 3D 框各自的 setter 方法设置 3D 框的属性 Width、Height 和 Depth ,如以下代码块所示

//Setting the properties of the Box 
box.setWidth(200.0); 
box.setHeight(400.0);   
box.setDepth(200.0);

第 3 步:创建 Group 对象 

在 start() 方法中,通过实例化名为 Group 的类来创建 group 对象,该类属于包 javafx.scene 。将上一步中创建的 Box(节点)对象作为参数传递给 Group 类的构造函数。为了将其添加到组中,应执行此作,如下所示

Group root = new Group(box);

第 4 步:启动应用程序

例1

下面是一个使用 JavaFX 生成 3D 框的程序。将此代码保存在名为 BoxExample.java 的文件中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Box; 
import javafx.stage.Stage; 
         
public class BoxExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Box 
      Box box = new Box();  
      
      //Setting the properties of the Box 
      box.setWidth(200.0); 
      box.setHeight(400.0);   
      box.setDepth(200.0); 
         
      //Creating a Group object  
      Group root = new Group(box); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);   
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Box"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   }
   public static void main(String args[]){ 
      launch(args); 
   } 
}

例2 

在前面的示例中,我们没有指定要从中绘制框的开始和结束坐标。但是,使用 animation 类的 translateX 和 translateY 属性,我们可以在 JavaFX 应用程序上重新定位该框。让我们看一下下面的示例,并将其保存在名为 TranslateBoxExample.java 的文件中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.Box;
import javafx.scene.paint.Color;
import javafx.scene.transform.Translate;
import javafx.stage.Stage; 
         
public class TranslateBoxExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Box 
      Box box = new Box();  
      
      //Setting the properties of the Box 
      box.setWidth(200.0); 
      box.setHeight(200.0);  
      box.setDepth(200.0);

      Translate translate = new Translate();       
      translate.setX(200); 
      translate.setY(150); 
      translate.setZ(25); 
      
      box.getTransforms().addAll(translate);
	  
      //Creating a Group object  
      Group root = new Group(box); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 400, 300);
	  
      scene.setFill(Color.web("#81c483"));	  
      
      //Setting title to the Stage 
      stage.setTitle("Translate a Box"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   }
   public static void main(String args[]){ 
      launch(args); 
   } 
}

JavaFX - 创建圆柱体

圆柱体是一种封闭的实体,具有两个平行(大部分为圆形)的底面,由曲面连接。为了可视化,您可以将 3D 圆柱体视为一堆杂乱的 2D 圆圈,这些圆圈彼此堆叠到一定高度;因此,即使它由两个参数描述,也使其成为三维形状。

绘制 3D 圆柱体的步骤

第 1 步:创建类

通过实例化名为 Cylinder 的类,在 JavaFX 中创建一个 Cylinder 对象,该类属于包 javafx.scene.shape 。您=可以在 start() 方法中实例化此类,如下所示  

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the Cylinder class       
      Cylinder cylinder = new Cylinder();   
   }    
}

第 2 步:为 Cylinder 设置属性

//Setting the properties of the Cylinder 
cylinder.setHeight(300.0f); 
cylinder.setRadius(100.0f); 

 第 3 步:创建 Group 对象 

Group root = new Group(cylinder);

 第 4 步:启动应用程序

例1

下面的程序演示如何使用 JavaFX 生成 Cylinder。将此代码保存在名为 CylinderExample.java 的文件中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.CullFace; 
import javafx.scene.shape.Cylinder; 
import javafx.stage.Stage;

public class CylinderExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Cylinder 
      Cylinder cylinder = new Cylinder(); 
         
      //Setting the properties of the Cylinder 
      cylinder.setHeight(300.0f); 
      cylinder.setRadius(100.0f); 
               
      //Creating a Group object  
      Group root = new Group(cylinder); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a cylinder"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

 例2

还可以对 3D 形状应用转换。在此示例中,我们尝试在 3D 圆柱体上应用平移转换,并在应用程序上重新定位它。将此代码保存在名为 TranslateCylinderExample.java 的文件中。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.scene.paint.Color;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class TranslateCylinderExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Cylinder 
      Cylinder cylinder = new Cylinder(); 
         
      //Setting the properties of the Cylinder 
      cylinder.setHeight(150.0f); 
      cylinder.setRadius(100.0f);

      Translate translate = new Translate();       
      translate.setX(200); 
      translate.setY(150); 
      translate.setZ(25); 
      
      cylinder.getTransforms().addAll(translate);	  
               
      //Creating a Group object  
      Group root = new Group(cylinder); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 400, 300); 
	  
      scene.setFill(Color.web("#81c483"));
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a cylinder"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

JavaFX - 创建球体

绘制 3D 球体的步骤

步骤 1:创建球体

public class ClassName extends Application { 
  @Override     
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the class Sphere 
      Sphere sphere = new Sphere();   
   }
}

第 2 步:为球体设置属性

//Setting the radius of the Sphere 
sphere.setRadius(300.0);

 第 3 步:创建 Group 对象 

Group root = new Group(sphere);

 第 4 步:启动应用程序

例1

以下程序演示如何使用 JavaFX 生成 Sphere。将此代码保存在名为 SphereExample.java 的文件中

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.Sphere; 
         
public class SphereExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Sphere  
      Sphere sphere = new Sphere();  
      
      //Setting the properties of the Sphere 
      sphere.setRadius(50.0);   
       
      sphere.setTranslateX(200); 
      sphere.setTranslateY(150);      
       
      //Creating a Group object  
      Group root = new Group(sphere); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Sphere - draw fill");
      
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

例2 

 在下面的程序中,我们通过为 JavaFX 应用程序的场景着色来在 JavaFX 中应用一些 CSS。将此代码保存在名为 CSSSphereExample.java 的文件中

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere; 
         
public class CSSSphereExample extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing a Sphere  
      Sphere sphere = new Sphere();  
      
      //Setting the properties of the Sphere 
      sphere.setRadius(50.0);   
       
      sphere.setTranslateX(100); 
      sphere.setTranslateY(150);      
       
      //Creating a Group object  
      Group root = new Group(sphere); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);
      
	  scene.setFill(Color.ORANGE);	  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Sphere");
      
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/964096.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

arm-linux-gnueabihf安装

Linaro Releases windows下打开wsl2中的ubuntu,资源管理器中输入: \\wsl$gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz 复制到/home/ark01/tool 在 Ubuntu 中创建目录: /usr/local/arm,命令如下: …

【双指针题目】

双指针 美丽区间&#xff08;滑动窗口&#xff09;合并数列&#xff08;双指针的应用&#xff09;等腰三角形全部所有的子序列 美丽区间&#xff08;滑动窗口&#xff09; 美丽区间 滑动窗口模板&#xff1a; int left 0, right 0;while (right < nums.size()) {// 增大…

【汽车电子软件架构】AutoSAR从放弃到入门专栏导读

本文是汽车电子软件架构&#xff1a;AutoSAR从放弃到入门专栏的导读篇。文章延续专栏文章的一贯作风&#xff0c;从概念与定义入手&#xff0c;希望读者能对AutoSAR架构有一个整体的认识&#xff0c;然后对专栏涉及的文章进行分类与链接。本文首先从AutoSAR汽车软件架构的概念&…

八、Spring Boot 日志详解

目录 一、日志的用途 二、日志使用 2.1 打印日志 2.1.1 在程序中获取日志对象 2.1.2 使用日志对象打印日志 2.2、日志框架介绍 2.2.1 门面模式(外观模式) 2.2.2 门面模式的实现 2.2.3 SLF4J 框架介绍 2.3 日志格式的说明 2.4 日志级别 2.4.1 日志级别的分类 2.4.2…

【Linux】24.进程信号(1)

文章目录 1. 信号入门1.1 进程与信号的相关知识1.2 技术应用角度的信号1.3 注意1.4 信号概念1.5 信号处理常见方式概览 2. 产生信号2.1 通过终端按键产生信号2.2 调用系统函数向进程发信号2.3 由软件条件产生信号2.4 硬件异常产生信号2.5 信号保存 3. 阻塞信号3.1 信号其他相关…

[Proteus仿真]基于51单片机的智能温控系统

[Proteus仿真]基于51单片机的智能温控系统 基于51单片机的智能温控系统&#xff1a;DS18B20精准测温LCD1602双屏显示三键设置上下限声光报警&#xff0c;支持温度校准、抗干扰设计、阈值记忆。 一.仿真原理图 ​​ 二.模块介绍 温度采集模块&#xff08;DS18B20&#xff0…

Windows下怎么安装FFFmpeg呢?

在Windows下使用Open-webui报错&#xff0c;说Couldnt find ffmpeg or avconv,解决open-webui报错Couldn‘t find ffmpeg or avconv-CSDN博客于是尝试解决问题&#xff0c;那么Windows下怎么安装FFFmpeg呢&#xff1f; 尝试了两种方法。 第一种方法pip安装&#xff08;失败&…

C基础寒假练习(2)

一、输出3-100以内的完美数&#xff0c;(完美数&#xff1a;因子和(因子不包含自身)数本身 #include <stdio.h>// 函数声明 int isPerfectNumber(int num);int main() {printf("3-100以内的完美数有:\n");for (int i 3; i < 100; i){if (isPerfectNumber…

【智力测试——二分、前缀和、乘法逆元、组合计数】

题目 代码 #include <bits/stdc.h> using namespace std; using ll long long; const int mod 1e9 7; const int N 1e5 10; int r[N], c[N], f[2 * N]; int nr[N], nc[N], nn, nm; int cntr[N], cntc[N]; int n, m, t;void init(int n) {f[0] f[1] 1;for (int i …

Vue-el挂载点

目录 一、Vue中的el挂载点是什么&#xff1f;二、Vue实例的作用范围是什么呢&#xff1f;三、Vue中的el是否可以挂载哪些选择器&#xff1f;四、el是否可以设置其他的dom元素呢&#xff1f; 一、Vue中的el挂载点是什么&#xff1f; el是用来设置Vue实例挂载&#xff08;管理&a…

c语言练习【实现终端功能、dup2实现文件拷贝、read write文件加载到链表】

练习1&#xff1a;实现终端功能 请实现一个终端的功能&#xff0c;注意需要带有cd功能 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h>#define MAX_CM…

MySQL数据库环境搭建

下载MySQL 官网&#xff1a;https://downloads.mysql.com/archives/installer/ 下载社区版就行了。 安装流程 看b站大佬的视频吧&#xff1a;https://www.bilibili.com/video/BV12q4y1477i/?spm_id_from333.337.search-card.all.click&vd_source37dfd298d2133f3e1f3e3c…

1.2 基于深度学习的底层视觉技术

文章目录 高层视觉任务与底层视觉任务深度神经网络相对于传统方法的优势 高层视觉任务与底层视觉任务 计算机视觉中的任务包含高层视觉任务&#xff0c;底层视觉任务。高层视觉任务是处理语义级别相关的任务&#xff0c;例如图像分类、目标检测、图像分割等。底层视觉任务处理与…

YOLOV11-1:YoloV11-安装和CLI方式训练模型

YoloV11-安装和CLI方式训练模型 1.安装和运行1.1安装的基础环境1.2安装yolo相关组件1.3命令行方式使用1.3.1 训练1.3.2 预测 本文介绍yoloV11的安装和命令行接口 1.安装和运行 1.1安装的基础环境 GPU环境&#xff0c;其中CUDA是12.4版本 1.2安装yolo相关组件 # 克隆github…

后盾人JS -- 原型

没有原型的对象 也有没有原型的对象 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document<…

【NEXT】网络编程——上传文件(不限于jpg/png/pdf/txt/doc等),或请求参数值是file类型时,调用在线服务接口

最近在使用华为AI平台ModelArts训练自己的图像识别模型&#xff0c;并部署了在线服务接口。供给客户端&#xff08;如&#xff1a;鸿蒙APP/元服务&#xff09;调用。 import核心能力&#xff1a; import { http } from kit.NetworkKit; import { fileIo } from kit.CoreFileK…

游戏引擎 Unity - Unity 下载与安装

Unity Unity 首次发布于 2005 年&#xff0c;属于 Unity Technologies Unity 使用的开发技术有&#xff1a;C# Unity 的适用平台&#xff1a;PC、主机、移动设备、VR / AR、Web 等 Unity 的适用领域&#xff1a;开发中等画质中小型项目 Unity 适合初学者或需要快速上手的开…

结构体DMA串口接收比特错位

发送&#xff1a; 显示&#xff1a; uint16_t接收时候会比特错位。

在线知识库的构建策略提升组织信息管理效率与决策能力

内容概要 在线知识库作为现代企业信息管理的重要组成部分&#xff0c;具有显著的定义与重要性。它不仅为组织提供了一个集中存储与管理知识的平台&#xff0c;还能够有效提升信息检索的效率&#xff0c;促进知识的创新和利用。通过这样的知识库&#xff0c;企业可以更好地应对…

e2studio开发RA2E1(4)----GPIO输出

e2studio开发RA2E1.4--GPIO输出 概述视频教学样品申请硬件准备参考程序源码下载新建工程工程模板保存工程路径芯片配置工程模板选择时钟设置GPIO口配置R_IOPORT_PinWrite()函数原型R_IOPORT_PortWrite()函数原型代码 概述 本篇文章主要介绍如何使用e2studio对瑞萨单片机进行GP…