Javafx-colors

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

JavaFX-色

アプリケーションに色を適用するために、JavaFXはパッケージ javafx.scene.paint パッケージでさまざまなクラスを提供します。 このパッケージには、Paintという名前の抽象クラスが含まれており、色を適用するために使用されるすべてのクラスの基本クラスです。

これらのクラスを使用すると、次のパターンで色を適用できます-

  • 均一-このパターンでは、ノード全体に均一に色が適用されます。

  • 画像パターン-これにより、ノードの領域を画像パターンで塗りつぶすことができます。

  • グラデーション-このパターンでは、ノードに適用される色は、ある点から別の点に変化します。 2種類のグラデーション、すなわち*線形グラデーション*と*放射グラデーション*があります。

    *Shape、Text* (Sceneを含む)など、色を適用できるすべてのノードクラスには、* setFill()*および* setStroke()*という名前のメソッドがあります。 これらは、それぞれノードとそのストロークの色の値を設定するのに役立ちます。

これらのメソッドは、Paintタイプのオブジェクトを受け入れます。 したがって、これらのタイプの画像のいずれかを作成するには、これらのクラスをインスタンス化し、オブジェクトをパラメーターとしてこれらのメソッドに渡す必要があります。

ノードに色を適用する

ノードに均一なカラーパターンを設定するには、次のように* setFill() setStroke()*メソッドにクラスカラーのオブジェクトを渡す必要があります-

//Setting color to the text
Color color = new Color.BEIGE
text.setFill(color);

//Setting color to the stroke
Color color = new Color.DARKSLATEBLUE
circle.setStroke(color);

上記のコードブロックでは、カラークラスの静的変数を使用してカラーオブジェクトを作成しています。

同様に、以下に示すように、RGB値またはカラーのHSB標準またはカラーのWebハッシュコードを使用することもできます-

//creating color object by passing RGB values
Color c = Color.rgb(0,0,255);

//creating color object by passing HSB values
Color c = Color.hsb(270,1.0,1.0);

//creating color object by passing the hash code for web
Color c = Color.web("0x0000FF",1.0);

以下は、JavaFXのノードに色を適用する方法を示す例です。 ここでは、円とテキストノードを作成し、それらに色を適用しています。

このコードを ColorExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class ColorExample extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing a Circle
      Circle circle = new Circle();

     //Setting the properties of the circle
      circle.setCenterX(300.0f);
      circle.setCenterY(180.0f);
      circle.setRadius(90.0f);

     //Setting color to the circle
      circle.setFill(Color.DARKRED);

     //Setting the stroke width
      circle.setStrokeWidth(3);

     //Setting color to the stroke
      circle.setStroke(Color.DARKSLATEBLUE);

     //Drawing a text
      Text text = new Text("This is a colored circle");

     //Setting the font of the text
      text.setFont(Font.font("Edwardian Script ITC", 50));

     //Setting the position of the text
      text.setX(155);
      text.setY(50);

     //Setting color to the text
      text.setFill(Color.BEIGE);
      text.setStrokeWidth(2);
      text.setStroke(Color.DARKSLATEBLUE);

     //Creating a Group object
      Group root = new Group(circle, text);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Color Example");

     //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 ColorExample.java
java ColorExample

実行すると、上記のプログラムは次のようにJavaFXウィンドウを生成します-

色の例

ノードへの画像パターンの適用

ノードに画像パターンを適用するには、 ImagePattern クラスをインスタンス化し、そのオブジェクトを* setFill() setStroke()*メソッドに渡します。

このクラスのコンストラクタは、6つのパラメータを受け入れます-

  • 画像-パターンの作成に使用する画像のオブジェクト。
  • xおよびy -アンカー長方形の原点の(x、y)座標を表す二重変数。
  • 高さと幅-パターンの作成に使用される画像の高さと幅を表す二重変数。
  • isProportional -これはブール変数です。このプロパティをtrueに設定すると、開始位置と終了位置が比例するように設定されます。
ImagePattern radialGradient = new ImagePattern(dots, 20, 20, 40, 40, false);

以下は、JavaFXのノードにイメージパターンを適用する方法を示す例です。 ここでは、円とテキストノードを作成し、それらに画像パターンを適用しています。

このコードを ImagePatternExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;

import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.paint.Stop;

import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class ImagePatternExample extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing a Circle
      Circle circle = new Circle();

     //Setting the properties of the circle
      circle.setCenterX(300.0f);
      circle.setCenterY(180.0f);
      circle.setRadius(90.0f);

     //Drawing a text
      Text text = new Text("This is a colored circle");

     //Setting the font of the text
      text.setFont(Font.font("Edwardian Script ITC", 50));

     //Setting the position of the text
      text.setX(155);
      text.setY(50);

     //Setting the image pattern
      String link = "https://encrypted-tbn1.gstatic.com"
         + "/images?q=tbn:ANd9GcRQub4GvEezKMsiIf67U"
         + "rOxSzQuQ9zl5ysnjRn87VOC8tAdgmAJjcwZ2qM";

      Image image = new Image(link);
      ImagePattern radialGradient = new ImagePattern(image, 20, 20, 40, 40, false);

     //Setting the linear gradient to the circle and text
      circle.setFill(radialGradient);
      text.setFill(radialGradient);

     //Creating a Group object
      Group root = new Group(circle, text);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Image pattern Example");

     //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 ImagePatternExample.java
java ImagePatternExample

実行すると、上記のプログラムは次のようにJavaFXウィンドウを生成します-

画像パターンの例

線形グラデーションパターンの適用

ノードに線形グラデーションパターンを適用するには、 LinearGradient クラスをインスタンス化し、そのオブジェクトを* setFill()、setStroke()*メソッドに渡します。

このクラスのコンストラクタは、すなわち、5つのパラメータを受け入れます-

  • startX、startY -これらのdoubleプロパティは、グラデーションの開始点のxおよびy座標を表します。
  • endX、endY -これらのdoubleプロパティは、グラデーションの終点のxおよびy座標を表します。
  • cycleMethod -この引数は、開始点と終了点で定義されたカラーグラデーション境界の外側の領域を塗りつぶす方法を定義します。
  • 比例-これはブール変数です。このプロパティを true に設定すると、開始位置と終了位置が比率に設定されます。
  • ストップ-この引数は、グラデーションラインに沿ったカラーストップポイントを定義します。
//Setting the linear gradient
Stop[] stops = new Stop[] {
   new Stop(0, Color.DARKSLATEBLUE),
   new Stop(1, Color.DARKRED)
};
LinearGradient linearGradient =
   new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);

以下は、JavaFXのノードにグラデーションパターンを適用する方法を示す例です。 ここでは、円とテキストノードを作成し、それらに線形グラデーションパターンを適用しています。

このコードを LinearGradientExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;

import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;

import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class LinearGradientExample extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing a Circle
      Circle circle = new Circle();

     //Setting the properties of the circle
      circle.setCenterX(300.0f);
      circle.setCenterY(180.0f);
      circle.setRadius(90.0f);

     //Drawing a text
      Text text = new Text("This is a colored circle");

     //Setting the font of the text
      text.setFont(Font.font("Edwardian Script ITC", 55));

     //Setting the position of the text
      text.setX(140);
      text.setY(50);

     //Setting the linear gradient
      Stop[] stops = new Stop[] {
         new Stop(0, Color.DARKSLATEBLUE),
         new Stop(1, Color.DARKRED)
      };
      LinearGradient linearGradient =
         new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);

     //Setting the linear gradient to the circle and text
      circle.setFill(linearGradient);
      text.setFill(linearGradient);

     //Creating a Group object
      Group root = new Group(circle, text);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Linear Gradient Example");

     //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 LinearGradientExample.java
java LinearGradientExample

実行すると、上記のプログラムは次のようにJavaFXウィンドウを生成します-

線形グラデーション

放射状グラデーションパターンの適用

放射状グラデーションパターンをノードに適用するには、 GradientPattern クラスをインスタンス化し、そのオブジェクトを* setFill()、setStroke()*メソッドに渡します。

このクラスのコンストラクタはいくつかのパラメータを受け入れますが、そのうちのいくつかは-

  • startX、startY -これらのdoubleプロパティは、グラデーションの開始点のxおよびy座標を表します。
  • endX、endY -これらのdoubleプロパティは、グラデーションの終点のxおよびy座標を表します。
  • cycleMethod -この引数は、カラーグラデーション境界の外側の領域が開始点と終了点によってどのように定義され、どのように塗りつぶされるべきかを定義します。
  • 比例-これはブール変数です。このプロパティを true に設定すると、開始位置と終了位置が比率に設定されます。
  • ストップ-この引数は、グラデーションラインに沿ったカラーストップポイントを定義します。
//Setting the radial gradient
Stop[] stops = new Stop[] {
   new Stop(0.0, Color.WHITE),
   new Stop(0.3, Color.RED),
   new Stop(1.0, Color.DARKRED)
};

RadialGradient radialGradient =
   new RadialGradient(0, 0, 300, 178, 60, false, CycleMethod.NO_CYCLE, stops);

以下は、JavaFXのノードに放射状のグラデーションパターンを適用する方法を示す例です。 ここでは、円とテキストノードを作成し、それらにグラデーションパターンを適用しています。

このコードを RadialGradientExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;

import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;

import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class RadialGradientExample extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing a Circle
      Circle circle = new Circle();

     //Setting the properties of the circle
      circle.setCenterX(300.0f);
      circle.setCenterY(180.0f);
      circle.setRadius(90.0f);

     //Drawing a text
      Text text = new Text("This is a colored circle");

     //Setting the font of the text
      text.setFont(Font.font("Edwardian Script ITC", 50));

     //Setting the position of the text
      text.setX(155);
      text.setY(50);

     //Setting the radial gradient
      Stop[] stops = new Stop[] {
         new Stop(0.0, Color.WHITE),
         new Stop(0.3, Color.RED),
         new Stop(1.0, Color.DARKRED)
      };
      RadialGradient radialGradient =
         new RadialGradient(0, 0, 300, 178, 60, false, CycleMethod.NO_CYCLE, stops);

     //Setting the radial gradient to the circle and text
      circle.setFill(radialGradient);
      text.setFill(radialGradient);

     //Creating a Group object
      Group root = new Group(circle, text);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting title to the Stage
      stage.setTitle("Radial Gradient Example");

     //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 RadialGradientExample.java
java RadialGradientExample

実行すると、上記のプログラムは次のようにJavaFXウィンドウを生成します-

放射状グラデーション