我试着每次按下图像时都要点亮,万一再次被按下,我希望它转到原来的照明点上。
// Effect To light up the image once it been pressed to green
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));
// Effect to show the unavailable images which can't be pressed
Lighting lighting_red = new Lighting();
lighting_red.setDiffuseConstant(1.0);
lighting_red.setSpecularConstant(0.0);
lighting_red.setSpecularExponent(0.0);
lighting_red.setSurfaceScale(0.0);
lighting_red.setLight(new Light.Distant(45, 45, Color.RED));
// the original effect and the one to change back the green effect once it being pressed again
Lighting orginalLighting = new Lighting();
orginalLighting.setDiffuseConstant(1.0);
orginalLighting.setSpecularConstant(0.0);
orginalLighting.setSpecularExponent(0.0);
orginalLighting.setSurfaceScale(0.0);
orginalLighting.setLight(new Light.Distant(85, 85, Color.LIGHTGREY));
// To initialize the original imageview and set its original effect
for(int i = 0;i<30;i++){
seats[i] = new ImageView(seats_image);
seats[i].setEffect(orginalLighting);
}
for(int i=0;i<30;i++){
Node seat = seats[i];
seat.setOnMouseClicked(e->{
if(seat.getEffect()!=lighting_red){
seat.setEffect(lighting); }
if(seat.getEffect()==lighting){
seat.setEffect(orginalLighting); }
});
}我想改变图像效果,以防如果不是红色到绿色。如果我已经按下它,我再次按它达到原来的效果,但不知怎么的,一旦我按下任何图像,什么都不会改变。
提示:如果我删除第二个if,图像将更改为绿色,如果我按下它,只要它不是红色。但是,有一次,我添加了第二个,如果什么都没有发生,似乎每次我按下它,就会改变原来的样子,图像中的任何东西都不会改变。
发布于 2016-12-04 19:33:44
你的逻辑是错的。您目前的实现是:
如果当前效果为originalLighting,则第一个if条件将为真,因此将效果更改为lighting。然后,第二个if条件也将为true (因为效果现在是lighting),因此您立即将效果更改为originalLighting。
你需要这样的东西:
if(seat.getEffect() == lighting) {
seat.setEffect(originalLighting);
} else if (seat.getEffect() == originalLighting) {
seat.setEffect(lighting);
}(请注意,如果正确地列出代码,则这些错误更容易查看和修复。)
发布于 2016-12-04 19:27:08
创建一个List<Integer>来存储每个图像的ID,具体取决于索引,每次按下该图像时,您都运行一个方法来检查ID,然后根据ID和按下图像的次数进行更改:
1)-if图像处于原始状态,然后增加1的列表并应用效果。
2)-if图像已经被按下,你减少了1,你应用效果。
//Create and initialize the List with a loop (length 30 images here)
List<Integer> pressCount = new ArrayList<>();https://stackoverflow.com/questions/40962413
复制相似问题