我创建要由用户选择的图像原始图像。我希望图像在用户点击后改变颜色
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);
点击后,我试图将图像更改为绿色:
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);
});
但我一直变得奇怪的颜色
原始图片:
我点击后得到的奇怪结果:
答案 0 :(得分:5)
您使用了错误的效果。 ColorAdjust相对于每个像素的当前值改变每个像素的亮度,对比度等。这有助于使图像更暗,但不能使图像变绿。
此外,获取黄色像素的亮度值并添加绿色亮度值是没有意义的。在大多数情况下,结果将是最高亮度值。
相反,您可以使用照明效果。这可能符合您的需求。此效果模拟图像前面的(彩色)光源。使用绿色环境光使图像变为绿色。
我做了一个例子:
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);
}
}