使用 JSlider 更改图像亮度


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,我用它来改变图像的亮度。 它工作得很好,图像变得更亮。我遇到的问题是,我以何种方式移动滑块并不重要,因为无论哪种方式它都会变得更亮。 我希望它的工作方式是,只有当滑块向右移动时,图像才会变得更亮。

你从不使用val的值。为什么不做这样的事情:图像亮度滑块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(,因此不能再添加任何蓝色来增加亮度。

最新更新