0
        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();
                }
            }
        }
    });

So this is my ChangeListener which I use to change the brightness of the image. It works decent and the image gets brighter. The problem I'm having is that it doesn't matter what way I move the slider because it gets brighter either way. The way I want it work is that the image should only get brighter when the slider is moved to the right.

Nudd3
  • 29
  • 4

1 Answers1

1

You never use the value of val. Why not do something like this: 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());
    }
}

I tested this on a background, not pixel by pixel, and the starting color was blue. You would have to change the code above based on the starting color, because brightness is increased by adding more color. Blue has a starting value of (0, 0, 255), so you cannot add any more blue to increase brightness.

  • Can't get it to work. For me it doesn't change the brightness it just turns the image black.. – Nudd3 May 18 '18 at 16:55