0

I am trying to increase the values of a 2D array using a slider. I essentially want to increase the brightness of an image that is written in by R G and B arrays.I believe my method is called correctly and is listening for the slider value,but I am now concerned about values exceeding 255. I added in modulus operators to prevent this but I am receiving an ArrayOutOfBoundsException starting at the line where I begin adding the amount to the index. I added print statements to see if the values change and they don't, but whenever I move my slider the ArrayOutOfBoundsException appears.

adjustBrightness Method to change values of the arrays:

     public void adjustBrightness(int amount) {

        int gimmieRows = thePPMImage.getNumRows();
        int gimmieCols = thePPMImage.getNumCols();
        int[][] gimmieRed = thePPMImage.getRedPixels();
        int[][] gimmieBlue = thePPMImage.getBluePixels();
        int[][] gimmieGreen = thePPMImage.getGreenPixels();
        int[][] brighterRedPixels =new int[gimmieRed.length][gimmieRed[0].length];
        int [][] brighterGreenPixels = new int[gimmieGreen.length][gimmieGreen.length];
        int [][] brighterBluePixels = new int[gimmieBlue.length][gimmieBlue[0].length];


        for (int i = 0; i < gimmieRows; i++) {
            for (int j = 0; j < gimmieCols; j++) {
                  if ((i+amount)%255>1.0 || (j+amount)%255>1.0) {
                      amount =amount-255;}
                  else  ((i+amount)%255<=1.0 || (j+amount)%255<=1.0)
                        {amount=amount;}
                System.out.println(gimmieRed[11][7]);

                brighterRedPixels[i][j] = amount + gimmieRed[i][j];
                brighterGreenPixels[i][j] = amount + gimmieGreen[i][j];
                brighterBluePixels[i][j] = amount + gimmieBlue[i][j];

                System.out.println(gimmieRed[11][7]);
                System.out.println(brighterRedPixels[11][7]);


            }

        } 
        thePPMImage = new PPMImage(brighterRedPixels, brighterGreenPixels,brighterBluePixels );
                thePPMImage.setRedPixels(brighterRedPixels);
                thePPMImage.setGreenPixels(brighterGreenPixels);
                thePPMImage.setBluePixels(brighterBluePixels);
                imageLabel.setIcon(new SImage(
                        transposeArray(thePPMImage.getRedPixels()),
                        transposeArray(thePPMImage.getGreenPixels()),
                        transposeArray(thePPMImage.getBluePixels())));

Change Listener for the slider.

public class event implements ChangeListener {
     public void stateChanged (ChangeEvent e){

        int amount = brightnessSlider.getValue();
        adjustBrightness(amount);

Any insight would be appreciated, this assignment was due like yesterday.

ashley
  • 1
  • 3
  • the "else if" statement is there because I have some coding for other JMenu items above the one for adjustBrightnessJmenuItem – ashley Dec 13 '15 at 10:16
  • 1
    *"but when I try to call it I get a error about a dereferenced int."* 1) Always copy/paste error and exception output! 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 4) It seems a bad idea to be repeatedly creating GUI components when an action happens. Declare them as class attributes, and create them if null. Otherwise simply display the current instances, using whatever values are needed. – Andrew Thompson Dec 13 '15 at 12:16
  • @Andrew ok. Thanks for the helpful hints. I was able to call my method. The exception I am referring to now is the ArrayOutOfBoundsException. I believe it is because the values of my indexes are exceeding 255 once I move my slider. I included modulus operators within an if statement, but I am still at square 1. – ashley Dec 13 '15 at 17:56
  • There's still no MCVE and your not copy/pasting error output. Perhaps someone else can help you, but I'm not investing more time in helping a person who ignores my advice. – Andrew Thompson Dec 14 '15 at 01:57

1 Answers1

0

The only reason for an ArrayOutOfBoundsException I currently see, is the println of gimmieRed[11][7] and brighterRedPixels[11][7]. If one of these arrays does not have 12 rows or 8 columns, an exception would be thrown.

It would help if you added (simplified) versions of the PPMImage & SImage classes and of the transposeArray method.

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28