多读书多实践,勤思考善领悟

JavaFX effect ColorAdjust(颜色调整效果)

本文于1665天之前发表,文中内容可能已经过时。

您可以通过对图像应用颜色调整效果来调整图像的颜色。这包括调整每个像素上的色调,饱和度,亮度对比度

javafx.scene.effect的名为ColorAdjust的类表示颜色调整效果,该类包含五个属性,即 -

  • input - 此属性属于Effect类型,它表示颜色调整效果的输入。
  • brightness - 此属性为Double类型,它表示此效果的亮度调整值。
  • 对比度 - 此属性为Double类型,表示此效果的对比度调整值。
  • hue - 此属性为Double类型,它表示此效果的色调调整值。
  • saturation - 此属性为Double类型,它表示此效果的饱和度调整值。

以下程序是演示颜色调整效果的示例。在这里,我们使用ImageImageView类在JavaFX场景中嵌入一个图像(Tutorialspoint Logo)。这是在位置100,70处完成的,并且配合高度和配合宽度分别为200和400。

我们正在使用颜色调整效果调整此图像的颜色。随着对比度,色调,亮度和饱和度值0.4。-0.05,0.9,0.8。

将此代码保存在名为ColorAdjustEffectExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class ColorAdjustEffectExample extends Application {
@Override
public void start(Stage stage) {
//Creating an image
Image image = new Image("https://www.ljjyy.com/img/logo.png");

//Setting the image view
ImageView imageView = new ImageView(image);

//Setting the position of the image
imageView.setX(100);
imageView.setY(70);

//setting the fit height and width of the image view
imageView.setFitHeight(200);
imageView.setFitWidth(400);

//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);

//Instantiating the ColorAdjust class
ColorAdjust colorAdjust = new ColorAdjust();

//Setting the contrast value
colorAdjust.setContrast(0.4);

//Setting the hue value
colorAdjust.setHue(-0.05);

//Setting the brightness value
colorAdjust.setBrightness(0.9);

//Setting the saturation value
colorAdjust.setSaturation(0.8);

//Applying coloradjust effect to the ImageView node
imageView.setEffect(colorAdjust);

//Creating a Group object
Group root = new Group(imageView);

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

//Setting title to the Stage
stage.setTitle("Coloradjust effect 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文件。

1
2
javac ColorAdjustEffectExample.java 
java ColorAdjustEffectExample

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

ColorAdjust