Javafx-2dshapes-rounded-rectangle

提供:Dev Guides
移動先:案内検索

JavaFX-2Dシェイプの角丸長方形

JavaFXでは、次の図に示すように、鋭いエッジまたはアーチ型のエッジを持つ長方形を描画できます。

角丸長方形

アーチ型のエッジを持つものは、丸みを帯びた長方形として知られ、それは2つの追加のプロパティがあります-

  • arcHeight -角丸長方形の角での円弧の垂直直径。
  • arcWidth -角丸長方形の角での円弧の水平直径。

アーク幅高さ

デフォルトでは、それぞれのセッターメソッド* setArcHeight()および setArcWidth()*を使用して円弧の高さと幅を+ ve値(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 RoundedRectangle.java
java RoundedRectangle

実行時に、上記のプログラムは、以下に示すように角丸長方形を表示するJavaFXウィンドウを生成します。

角丸四角形の描画