简体   繁体   中英

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. 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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