JavaFX - 線性漸變圖案



除了純色,您還可以在 JavaFX 中顯示顏色漸變。在色彩科學中,顏色漸變定義為顏色根據其位置而變化的過渡。因此,顏色漸變也稱為顏色漸變或顏色過渡。

傳統上,顏色漸變包含按順序或線性排列的顏色。但是,線上性漸變圖案中,顏色沿單一方向流動。即使要著色的形狀不是線性的,例如圓形或橢圓形,顏色仍然會沿一個方向排列。

讓我們在本節中學習如何在二維形狀上應用線性漸變圖案。

應用線性漸變圖案

要將線性漸變圖案應用於節點,請例項化LinearGradient類並將它的物件傳遞給setFill(),setStroke()方法。

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

  • startX, startY - 這些雙精度屬性表示漸變起點的 x 和 y 座標。

  • endX, endY - 這些雙精度屬性表示漸變終點的 x 和 y 座標。

  • cycleMethod - 此引數定義如何填充漸變邊界(由起點和終點定義)之外的區域。

  • proportional - 這是一個布林變數;將此屬性設定為true時,起點和終點位置將設定為比例。

  • Stops - 此引數定義沿漸變線的顏色停止點。

//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); 

示例 1

以下示例演示如何在 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 --module-path %PATH_TO_FX% --add-modules javafx.controls LinearGradientExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls LinearGradientExample

輸出

執行上述程式後,將生成如下 JavaFX 視窗:

Linear Gradient

示例 2

除了圓形之外,您還可以將這種型別的漸變應用於其他封閉形狀,例如多邊形。在這裡,我們正在建立一個三角形並使用某種線性漸變圖案為其著色。

將此程式碼儲存在名為TriangleLinearGradient.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.Polygon;
         
public class TriangleLinearGradient extends Application { 
   @Override 
   public void start(Stage stage) {            
      Polygon triangle = new Polygon(); 
	  
      triangle.getPoints().addAll(new Double[]{ 
         100.0, 50.0, 
         170.0, 150.0, 
         100.0, 250.0,  
      }); 
       
        
      Stop[] stops = new Stop[] { 
         new Stop(0, Color.ORANGE),  
         new Stop(1, Color.YELLOW)
      };  
      LinearGradient linearGradient = 
         new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops); 
       

      triangle.setFill(linearGradient);  
          
      Group root = new Group(triangle); 
          
      Scene scene = new Scene(root, 300, 300);  
      
      stage.setTitle("Linear Gradient Example"); 
          
      stage.setScene(scene); 
          
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}

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

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

輸出

執行上述程式後,將生成如下 JavaFX 視窗:

Triangle Linear Gradient
廣告