简体   繁体   English

javafx图像颜色变化

[英]javafx image color change

Im creating raws of images to be selected by the user. 我正在创建要由用户选择的图像原始文件。 I want the image to change its colour once the user clicks on it 我希望图像在用户单击后更改其颜色

FileInputStream seats_fileInputStream = new FileInputStream("seat.png");
Image seats_image = new Image(seats_fileInputStream,50,50,false,false);

ImageView[] seats = new ImageView[30];

for(int i = 0;i<30;i++){
seats[i] = new ImageView(seats_image);
}

HBox seatsRaw_hbox[] = new HBox[5];
VBox seatsLine_vbox = new VBox();

int seatsCount = 0;
for(int i=0;i<5;i++){
  seatsRaw_hbox[i]= new HBox();
     for(int j=0;j<6;j++){       
   seatsRaw_hbox[i].getChildren().addAll(seats[seatsCount]);
   seatsCount++;
     }
   seatsLine_vbox.getChildren().add(seatsRaw_hbox[i]);

            } 

      BorderPane Test = new BorderPane();
      Test.setCenter(seatsLine_vbox);

Im trying to change the image to green color once it being click: 我试图将图像单击后更改为绿色:

Color targetColor = Color.GREEN;
    ColorAdjust colorAdjust = new ColorAdjust();
     colorAdjust.setSaturation(targetColor.getSaturation()); 
    colorAdjust.setHue(targetColor.getHue());
    colorAdjust.setBrightness(targetColor.getBrightness());


    seats[0].setOnMouseClicked(e->{
        seats[0].setEffect(colorAdjust);       
    });

But I keep getting strange colors 但是我一直在变奇怪的颜色

The Orginal image: 原始图片:

在此处输入图片说明

The strange result I got once I click: 单击后得到的奇怪结果:

在此处输入图片说明

You are using the wrong effect for your purpose. 您出于目的使用了错误的效果。 ColorAdjust changes the brightness, contrast, etc. of each pixel relative to the current value of each pixel. ColorAdjust相对于每个像素的当前值更改每个像素的亮度,对比度等。 This is useful to make picture darker, but not to make it green. 这有助于使图片变暗,但不能使图片变绿。

Additionally it does not make sense to take the brightness value of a yellow pixel and add the brightness amount of a green one to it. 另外,获取黄色像素的亮度值并向其添加绿色像素的亮度量没有意义。 In most cases the result would be the highest possible brightness value. 在大多数情况下,结果将是可能的最高亮度值。

Instead you could use the Lighting effect. 相反,您可以使用照明效果。 This may fits your needs. 这可能适合您的需求。 This effect simulates a (colored) lighting source in front of the image. 此效果模拟图像前面的(彩色)光源。 Using a green ambient light makes the image green. 使用绿色环境光会使图像变绿。

I made an example: 我举了一个例子:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class Main extends Application {

    @Override
    public void start(Stage stage) {
        HBox hbox = new HBox();

        ImageView imageView1 = new ImageView(new Image(Main.class.getResourceAsStream("/image.png")));
        ImageView imageView2 = new ImageView(new Image(Main.class.getResourceAsStream("/image.png")));
        hbox.getChildren().add(imageView1);
        hbox.getChildren().add(imageView2);

        Lighting lighting = new Lighting();
        lighting.setDiffuseConstant(1.0);
        lighting.setSpecularConstant(0.0);
        lighting.setSpecularExponent(0.0);
        lighting.setSurfaceScale(0.0);
        lighting.setLight(new Light.Distant(45, 45, Color.GREEN));

        imageView2.setEffect(lighting);

        stage.setScene(new Scene(hbox));
        stage.sizeToScene();
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM