目录

Color Input

颜色输入效果提供与绘制矩形并用颜色填充相同的输出。 与其他效果不同,如果此效果应用于任何节点,则它仅显示矩形框(而不是节点)。 此效果主要用于传递其他效果的输入。

例如,在应用混合效果时,它需要效果类型的对象作为输入。 在那里我们可以传递这个作为输入。

javafx.scene.effect名为ColorInput的类表示颜色输入效果。 该类包含四个属性,即 -

  • x - 此属性为双重类型; 它表示颜色输入位置的x坐标。

  • y - 此属性为双重类型; 它表示颜色输入位置的y坐标。

  • height - 此属性为double类型; 它代表要填充颜色的区域的高度。

  • width - 此属性为double类型; 它表示要用颜色填充的区域的宽度。

  • paint - 此属性为Paint类型; 它表示输入区域要填充的颜色。

例子 (Example)

以下是演示颜色输入效果的示例。 在这里,我们在位置50,140处创建尺寸为50,400(高度,宽度)的颜色输入,并用颜色CHOCOLATE填充它。

我们正在创建矩形并将此效果应用于它。 将此代码保存在名为ColorInputEffectExample.java.的文件中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 ColorInputEffectExample.java 
java ColorInputEffectExample 

执行时,上面的程序生成一个JavaFX窗口,如下所示。

颜色输入效果
↑回到顶部↑
WIKI教程 @2018