简体   繁体   中英

Using an EventHandler to increase values of an 2D array

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 RG 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM