slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int val = slider.getValue();
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
image.setPixel(x, y, image.getPixel(x, y).brighter());
frame.repaint();
}
}
}
});
这是我的 ChangeListener,我用它来更改图像的亮度。 它工作得很好,图像变得更亮。我遇到的问题是,无论我如何移动 slider 都没关系,因为无论哪种方式它都会变得更亮。 我希望它的工作方式是,只有当 slider 向右移动时,图像才会变得更亮。
最佳答案
你永远不会使用val的值。为什么不做这样的事情:image brightness slider Java
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
Color color = new Color(image.getRGB(x, y));
int r, g, b;
val = slider.getValue();
r = ((color.getRed() + (val/20)) % 255);
b = ((color.getBlue() + (val/20)));
g = ((color.getGreen() + (val/20)) % 255);
if(b > 255) b = 255;
color = new Color(r, g, b);
image.setRGB(x, y, color.getRGB());
}
}
我在背景上测试了它,而不是逐像素测试,起始颜色是蓝色。您必须根据起始颜色更改上面的代码,因为通过添加更多颜色来增加亮度。蓝色的起始值为 (0, 0, 255),因此您无法添加更多蓝色来增加亮度。
关于java - 使用 JSlider 更改图像亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50415387/