Javafx 简明教程

JavaFX - Color Adjust Effect

可以在任何图像上执行的最基本的效果是调整其颜色。各种图像处理应用程序都允许你这样做。同样,JavaFX 也允许你在应用程序中调整图像的颜色。

The most basic effect that can be performed on any image is adjusting its color. Various image processing applications allow you to do it. Similarly, JavaFX also allows you to adjust a color of an image in an application.

可以通过对其应用颜色调整效果来调整图像的颜色。这包括调整每个像素上的 Hue, Saturation, BrightnessContrast

You can adjust the color of an image by applying the color adjust effect to it. This includes the adjustment of the Hue, Saturation, Brightness and Contrast on each pixel.

javafx.scene.effect 中名为 ColorAdjust 的类表示颜色调整效果,此类包含五个属性,它们是:

The class named ColorAdjust of the package javafx.scene.effect represents the color adjust effect, this class contains five properties namely −

  1. input − This property is of the Effect type and it represents an input to the color adjust effect.

  2. brightness − This property is of Double type and it represents the brightness adjustment value for this effect.

  3. contrast − This property is of Double type and it represents the contrast adjustment value for this effect.

  4. hue − This property is of Double type and it represents the hue adjustment value for this effect.

  5. saturation − This property is of Double type and it represents the saturation adjustment value for this effect.

Example

以下程序演示颜色调整效果的示例。在此处,我们使用 ImageImageView 类将图像(Tutorialspoint 徽标)嵌入 JavaFX 场景。这是在位置 100, 70 以及拟合高度和拟合宽度分别为 200 和 400 完成的。

The following program is an example of demonstrating the color adjust effect. In here, we are embedding an image (Tutorialspoint Logo) in JavaFX scene using Image and ImageView classes. This is being done at the position 100, 70 and with a fit height and fit width of 200 and 400 respectively.

image view

我们使用颜色调整效果调整此图像的颜色。使用以下 contrast, hue, brightness and saturation 值:0.4. -0.05, 0.9, 0.8。

We are adjusting the color of this image using the color adjust effect. With contrast, hue, brightness and saturation values as 0.4. -0.05, 0.9, 0.8.

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

Save this code in a file with the name ColorAdjustEffectExample.java.

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("http://www.tutorialspoint.com/green/images/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 文件。

Compile and execute the saved java file from the command prompt using the following commands.

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

Output

执行后,上述程序会生成如下所示的 JavaFX 窗口。

On executing, the above program generates a JavaFX window as shown below.

coloradjust