JavaFX - 顏色輸入效果



顏色輸入效果與繪製矩形並用顏色填充它產生的輸出相同。與其他效果不同,如果將此效果應用於任何節點,它只會顯示一個矩形框(而不是節點)。此效果主要用作其他效果的輸入。

例如,在應用混合效果時,它需要一個效果型別的物件作為輸入。在那裡我們可以將其作為輸入傳遞。

名為ColorInputjavafx.scene.effect包中的類表示顏色輸入效果。此類包含四個屬性,即:

  • x - 此屬性為雙精度型別;它表示顏色輸入位置的 x 座標。

  • y - 此屬性為雙精度型別;它表示顏色輸入位置的 y 座標。

  • height - 此屬性為雙精度型別;它表示要填充顏色的區域的高度。

  • width - 此屬性為雙精度型別;它表示要填充顏色的區域的寬度。

  • paint - 此屬性為 Paint 型別;它表示要填充輸入區域的顏色。

示例

以下示例演示了顏色輸入效果。在這裡,我們建立了一個尺寸為 50、400(高度、寬度)的顏色輸入,位於 50、140 位置,並用 CHOCOLATE 顏色填充它。

我們正在建立矩形並將此效果應用於它。將此程式碼儲存在名為ColorInputEffectExample.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.ColorInput; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
         
public class ColorInputEffectExample extends Application { 
   @Override  
   public void start(Stage stage) {            
      //creating a rectangle 
      Rectangle rectangle = new Rectangle();
      
      //Instantiating the Colorinput class 
      ColorInput colorInput = new ColorInput();         
       
      //Setting the coordinates of the color input 
      colorInput.setX(50); 
      colorInput.setY(140); 
      
      //Setting the height of the region of the collor input 
      colorInput.setHeight(50); 
      
      //Setting the width of the region of the color input 
      colorInput.setWidth(400); 
      
      //Setting the color the color input 
      colorInput.setPaint(Color.CHOCOLATE);  
      
      //Applying coloradjust effect to the Rectangle 
      rectangle.setEffect(colorInput);    
         
      //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("Sample Application"); 
         
      //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 ColorInputEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ColorInputEffectExample 

輸出

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

Color Input Effect
廣告

© . All rights reserved.