使用EventHandler增加2D数组的值

时间:2015-12-13 10:14:37

标签: java arrays image swing event-handling

我正在尝试使用滑块增加2D数组的值。我本质上想要增加由RG和B数组写入的图像的亮度。我相信我的方法被正确调用并且正在监听滑块值,但我现在担心超过255的值。我在模数运算符中添加了为了防止这种情况,但我收到一个ArrayOutOfBoundsException,从我开始将数量添加到索引的行开始。我添加了print语句以查看值是否发生变化但是没有,但每当我移动滑块时,都会出现ArrayOutOfBoundsException。

adjustBrightness更改数组值的方法:

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

更改滑块的监听器。

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

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

任何见解都会受到赞赏,这项任务应该像昨天一样。

1 个答案:

答案 0 :(得分:0)

我目前看到ArrayOutOfBoundsException的唯一原因是gimmieRed[11][7]brighterRedPixels[11][7]的println。如果其中一个数组没有12行或8列,则会抛出异常。

如果您添加PPMImage&amp;的{简化版},则会有所帮助SImage类和transposeArray方法。