JavaFX - 繪製圓角矩形



在幾何學中,圓角矩形定義為一個 2D 形狀,它包含弧形邊而不是尖銳邊。它是透過在尖銳邊矩形上放置四個不同的圓圈(頂點作為圓心)來獲得的。這個圓角矩形的邊是針對這些圓圈繪製的公切線,如下面的影像所示。

Rounded Rectangle

在圓角矩形中,長度和寬度與包含在其內的尖銳邊矩形的尺寸相同。

JavaFX 中的圓角矩形

在 JavaFX 中,您可以繪製一個具有尖銳邊或弧形邊的矩形,如下面的圖表所示。

Rounded Rectangle

具有弧形邊的那個被稱為圓角矩形,因為它不包含頂點。此圖形主要用於設計和光學中的各種應用,並且具有兩個附加屬性,即:

  • arcHeight - 圓角矩形角處的弧的垂直直徑。

  • arcWidth - 圓角矩形角處的弧的水平直徑。

Arc Width Height

預設情況下,JavaFX 建立一個具有尖銳邊的矩形,除非您使用各自的 setter 方法 setArcHeight()setArcWidth() 將弧的高度和寬度設定為正值 (0<)。

繪製圓角矩形

為了在 JavaFX 中繪製圓角矩形,您還需要例項化名為 Rectangle 的類,該類位於 javafx.scene.shape 包中。除此之外,您還必須設定圓角矩形的弧屬性,如下所示:

rectangle.setX(150.0f); 
rectangle.setY(75.0f); 
rectangle.setWidth(300.0f); 
rectangle.setHeight(150.0f);

rectangle.setArcWidth(30.0); 
rectangle.setArcHeight(20.0); 

按照本教程的 繪製矩形 章節中提到的所有其他步驟,啟動帶有圓角矩形的 JavaFX 應用程式。

示例

以下程式使用 JavaFX 生成一個圓角矩形。將此程式碼儲存在名為 RoundedRectangle.java 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.shape.Rectangle; 
         
public class RoundedRectangle extends Application { 
   @Override 
   public void start(Stage stage) {         
      //Drawing a Rectangle 
      Rectangle rectangle = new Rectangle();  
      
      //Setting the properties of the rectangle 
      rectangle.setX(150.0f); 
      rectangle.setY(75.0f); 
      rectangle.setWidth(300.0f); 
      rectangle.setHeight(150.0f); 
       
      //Setting the height and width of the arc 
      rectangle.setArcWidth(30.0); 
      rectangle.setArcHeight(20.0);  
         
      //Creating a Group object  
      Group root = new Group(rectangle); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Rectangle");
      
      //Adding scene to the stage 
      stage.setScene(scene); 
      
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls RoundedRectangle.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls RoundedRectangle

輸出

執行上述程式後,將生成一個 JavaFX 視窗,其中顯示一個圓角矩形,如下所示。

Drawing Rounded Rectangle

示例

讓我們再看一個使用 JavaFX 繪製圓角矩形的示例。此外,讓我們在此示例中應用一些 CSS,例如新增背景顏色。將此程式碼儲存在名為 CSSRoundedRectangle.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.Rectangle; 
         
public class CSSRoundedRectangle extends Application { 
   @Override 
   public void start(Stage stage) {         
      //Drawing a Rectangle 
      Rectangle rectangle = new Rectangle();  
      
      //Setting the properties of the rectangle 
      rectangle.setX(50.0f); 
      rectangle.setY(75.0f); 
      rectangle.setWidth(200.0f); 
      rectangle.setHeight(150.0f); 
       
      //Setting the height and width of the arc 
      rectangle.setArcWidth(30.0); 
      rectangle.setArcHeight(20.0);

      //Colour the rounded rectangle
      rectangle.setFill(Color.DARKBLUE);	  
         
      //Creating a Group object  
      Group root = new Group(rectangle);
         
      //Creating a scene object 
      Scene scene = new Scene(root, 300, 300);	
      scene.setFill(Color.RED);		  
      
      //Setting title to the Stage 
      stage.setTitle("Drawing a Rectangle");
      
      //Adding scene to the stage 
      stage.setScene(scene); 
      
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls CSSRoundedRectangle.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls CSSRoundedRectangle

輸出

執行上述程式後,將生成一個 JavaFX 視窗,其中顯示一個圓角矩形,如下所示。

Drawing Rounded Rectangle
廣告

© . All rights reserved.