JavaFX - 顏色



當您在 JavaFX 應用程式中繪製 2D 形狀時,您可能已經注意到,預設情況下,它是黑色。但是,黑色並不總是適合使用者建立的所有型別的應用程式。因此,JavaFX 允許您將此預設顏色更改為使用者認為適合其應用程式的任何顏色。

為了將顏色應用於應用程式,JavaFX 在包 **javafx.scene.paint** 包中提供了各種類。此包包含一個名為 Paint 的抽象類,它是所有用於應用顏色的類的基類。

使用這些類,您可以按照以下模式應用顏色:

  • **均勻** - 在此模式下,顏色在整個節點中統一應用。

  • **影像圖案** - 這允許您使用影像圖案填充節點的區域。

  • **漸變** - 在此模式下,應用於節點的顏色從一個點到另一個點變化。它有兩種漸變,即 **線性漸變** 和 **徑向漸變**。

所有可以應用顏色的節點類,例如 **Shape、Text**(包括 Scene),都具有名為 **setFill()** 和 **setStroke()** 的方法。這些將分別幫助設定節點及其描邊的顏色值。

這些方法接受 Paint 型別的物件。因此,要建立任何這些型別的影像,您需要例項化這些類並將物件作為引數傳遞給這些方法。

將顏色應用於節點

要將均勻顏色圖案設定為節點,您需要將 Color 類的物件傳遞給 **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);

在上面的程式碼塊中,我們使用 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 --module-path %PATH_TO_FX% --add-modules javafx.controls ColorExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ColorExample

輸出

執行後,上述程式生成一個 JavaFX 視窗,如下所示:

Color Example

將影像圖案應用於節點

要將影像圖案應用於節點,請例項化 **ImagePattern** 類並將它的物件傳遞給 **setFill()**、**setStroke()** 方法。

此類的建構函式接受六個引數,即:

  • **Image** - 您要用來建立圖案的影像物件。

  • **x 和 y** - 表示錨定矩形原點 (x, y) 座標的雙精度變數。

  • **height 和 width** - 表示用於建立圖案的影像的高度和寬度的雙精度變數。

  • **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 --module-path %PATH_TO_FX% --add-modules javafx.controls ImagePatternExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ImagePatternExample 

輸出

執行後,上述程式生成一個 JavaFX 視窗,如下所示:

Image Pattern Example
廣告