📜  JavaFX |带示例的气缸

📅  最后修改于: 2022-05-13 01:55:23.245000             🧑  作者: Mango

JavaFX |带示例的气缸

Cylinder 是 JavaFX 的一部分。 Cylinder 类用于创建具有指定高度和半径的 3 维圆柱体。圆柱体以原点为中心。

该类的构造函数是

  1. Cylinder() :创建一个半径为 1.0、高度为 2.0 的 Cylinder 的新实例。
  2. Cylinder(double r, double h) :创建给定半径和高度的 Cylinder 的新实例。
  3. Cylinder(double r, double h, int div) :创建一个给定半径、高度和分度的 Cylinder 的新实例

常用方法

methodexplanation
getHeight()returns the height of the cylinder
getRadius()returns the radius of the base of cylinder
setHeight(double v)sets the height of the cylinder
setRadius(double v)sets the radius of the cylinder

用于创建圆柱体并将其添加到舞台的Java程序

该程序创建了一个名为圆柱体的圆柱体(高度和半径作为参数传递)。圆柱体将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加圆柱体。该组附加到场景中。最后调用 show() 方法显示最终结果。

Java
// Java program to create a cylinder
// and add it to the stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Cylinder;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
public class cylinder_0 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating cylinder");
 
        // create a cylinder
        Cylinder cylinder = new Cylinder(20.0f, 120.0f);
 
        // create a Group
        Group group = new Group(cylinder);
 
        // translate the cylinder to a position
        cylinder.setTranslateX(100);
        cylinder.setTranslateY(100);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


Java
// Java program to create a cylinder and add a perspective camera to it .
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Cylinder;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class cylinder_1 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating cylinder");
 
        // create a cylinder
        Cylinder cylinder = new Cylinder(20.0f, 120.0f);
 
        // create a Group
        Group group = new Group(cylinder);
 
        // translate the cylinder to a position
        cylinder.setTranslateX(100);
        cylinder.setTranslateY(100);
 
        // create a perspective camera
        PerspectiveCamera camera = new PerspectiveCamera(false);
        camera.setTranslateX(0);
        camera.setTranslateY(0);
        camera.setTranslateZ(0);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        scene.setCamera(camera);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


输出:

用于创建圆柱体并向其添加透视相机的Java程序

这个程序创建了一个名为圆柱体的圆柱体(高度和半径作为参数传递)。圆柱体将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加圆柱体。该组附加到场景中。最后,调用 show() 方法显示最终结果。将创建透视相机并将其添加到场景中,以 3D 渲染圆柱体。

Java

// Java program to create a cylinder and add a perspective camera to it .
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Cylinder;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class cylinder_1 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating cylinder");
 
        // create a cylinder
        Cylinder cylinder = new Cylinder(20.0f, 120.0f);
 
        // create a Group
        Group group = new Group(cylinder);
 
        // translate the cylinder to a position
        cylinder.setTranslateX(100);
        cylinder.setTranslateY(100);
 
        // create a perspective camera
        PerspectiveCamera camera = new PerspectiveCamera(false);
        camera.setTranslateX(0);
        camera.setTranslateY(0);
        camera.setTranslateZ(0);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        scene.setCamera(camera);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}

输出:

注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器

参考:

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Cylinder.html