📜  JavaFX | Dimension2D 类

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

JavaFX | Dimension2D 类

Dimension2D 类是 JavaFX 的一部分。 Dimension2D 类是一个存储高度和宽度的对象。它继承了Java.lang.Object类。

类的构造函数:

  1. Dimension2D(double width, double height) :创建具有指定宽度和高度的维度对象。

常用方法:

MethodExplanation
equals(Object obj)Returns whether the two Dimension2D object are equal or not
getHeight()Returns the height of the Dimension2d object
getWidth()Returns the width of the Dimension2d object
hashCode()Returns a hash code value for the Dimension2D object.

下面的程序将说明 Dimension2D 类的使用:

  1. 用于创建 Dimension2D 对象并显示其内容的Java程序:该程序创建一个名为 dimension 的 Dimension2D 对象。尺寸的高度和宽度作为参数传递。然后使用getHeight()getWidth()函数从对象中提取高度和宽度,并显示相应的值。
    // Java program to create a Dimension2D 
    // object and display its contents
    import javafx.geometry.*;
      
    public class Dimension_1 {
      
        // Main Method
        public static void main(String args[])
        {
      
            // create a dimension object
            Dimension2D dimension = new Dimension2D(20.0f, 50.0f);
      
            double height, width;
      
            // get the height and width of the dimension2D
            height = dimension.getHeight();
            width = dimension.getWidth();
      
            // display the height and width of dimension2D
            System.out.println("Height = " + height + " Width = " + width);
        }
    }
    

    输出:

    Height = 50.0 Width = 20.0
    

  2. 创建 3 个 Dimension2D 对象的Java程序显示其内容以及一个对象是否等于另一个对象:该程序创建三个 Dimension2D 对象,名为dimension_1dimension_2dimension_3 。尺寸的高度和宽度作为参数传递。然后使用getHeight()getWidth()函数从对象中提取高度和宽度,并显示相应的值。然后使用 equals() 函数比较这三个对象并显示结果。
    // Java program to create 3 Dimension2D objects display
    // its contents and how whether one object is equal to 
    // the other or not
    import javafx.geometry.*;
      
    public class Dimension_2 {
      
        // Main Method
        public static void main(String args[])
        {
      
            // create three dimension objects
            Dimension2D dimension_1 = new Dimension2D(20.0f, 50.0f);
            Dimension2D dimension_2 = new Dimension2D(120.0f, 150.0f);
            Dimension2D dimension_3 = new Dimension2D(20.0f, 50.0f);
      
            // display the coordinates of the 3 dimensions
            display(dimension_1);
            display(dimension_2);
            display(dimension_3);
      
            // check whether any dimension is equal to other or not
            System.out.println("Dimension 1 equals Dimension 2 = " 
                               + dimension_1.equals(dimension_2));
      
            System.out.println("Dimension 2 equals Dimension 3 = " 
                                + dimension_2.equals(dimension_3));
      
            System.out.println("Dimension 3 equals Dimension 1 = " 
                                + dimension_3.equals(dimension_1));
        }
      
        // Display Method
        public static void display(Dimension2D dimension)
        {
      
            double height, width;
      
            // get the cheight and width of the dimension
            height = dimension.getHeight();
            width = dimension.getWidth();
      
            // display the height and width of dimension2D
            System.out.println("Height = " + height + " Width = " + width);
        }
    }
    

    输出:

    Height = 50.0 Width = 20.0
    Height = 150.0 Width = 120.0
    Height = 50.0 Width = 20.0
    Dimension 1 equals Dimension 2 = false
    Dimension 2 equals Dimension 3 = false
    Dimension 3 equals Dimension 1 = true
    

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

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Dimension2D.html