title: How to expand a PPM Image in Java using RGB arrays
body: <p>I am working on a java project (creating my own image editior) and my task is to allow the user to double the size of a existing PPM image. I must do this through the use of 2-d arrays of RGB. My instructor gave me a hint that I must create a new image with double the width and height of the first one and new arrays to fill it. I believe I've done this correctly. However, when I run my program and click the double size button on my interface, my original image doubles in size, but it becomes completely black. I believe there is a problem with how I am transposing the RGB values. I inserted print statements to see when the code goes wrong( when my RGB values become zeros).... </p>
  
  <pre><code>else if (action.equals(doubleSizeMenuItem.getActionCommand())) {
      int gimmieRows = thePPMImage.getNumRows();
      int gimmieCols = thePPMImage.getNumCols();
      int[][] gimmieRed = thePPMImage.getRedPixels();
      int[][] gimmieBlue = thePPMImage.getBluePixels();
      int[][] gimmieGreen = thePPMImage.getGreenPixels();
      int[][] doubleMyRedPixels = new int[gimmieRed.length * 2][gimmieRed.length * 2];
      int[][] doubleMyBluePixels = new int[gimmieBlue.length * 2][gimmieBlue.length * 2];
      int[][] doubleMyGreenPixels = new int[gimmieGreen.length * 2][gimmieGreen.length * 2];
      int longer = gimmieRows * 2;
      int wider = gimmieCols * 2;
  
      for (int i = 0; i &lt; longer; i++) {
          for (int j = 0; i &lt; wider; j++) {
              doubleMyRedPixels[i * 2][j * 2]=gimmieRed[i][j];
              doubleMyRedPixels[i * 2][(j * 2) + 1]= gimmieRed[i][j];
              doubleMyRedPixels[(i * 2) + 1][j * 2]= gimmieRed[i][j];
              doubleMyRedPixels[(i * 2) + 1][(j * 2) + 1]= gimmieRed[i][j];
              doubleMyGreenPixels[i * 2][j * 2]=gimmieGreen[i][j];
              doubleMyGreenPixels[i * 2][(j * 2) + 1]= gimmieGreen[i][j];
              doubleMyGreenPixels[(i * 2) + 1][j * 2]= gimmieGreen[i][j];
              doubleMyGreenPixels[(i * 2) + 1][(j * 2) + 1]= gimmieGreen[i][j];
              System.out.println(doubleMyRedPixels[0][0]);
              doubleMyBluePixels[i * 2][j * 2]=gimmieBlue[i][j];
              doubleMyBluePixels[i * 2][(j * 2) + 1]= gimmieBlue[i][j];
              doubleMyBluePixels[(i * 2) + 1][j * 2]= gimmieBlue[i][j];
              doubleMyBluePixels[(i * 2) + 1][(j * 2) + 1]= gimmieBlue[i][j];
  
              thePPMImage=new PPMImage(doubleMyRedPixels, doubleMyGreenPixels, doubleMyBluePixels);
              System.out.println(doubleMyRedPixels[5][5]);
              thePPMImage.setRedPixels(doubleMyRedPixels);
              thePPMImage.setGreenPixels(doubleMyGreenPixels);
              thePPMImage.setBluePixels(doubleMyBluePixels);
              System.out.println(doubleMyRedPixels[2][7]);
              imageLabel.setIcon( new SImage(
                  transposeArray(thePPMImage.getRedPixels()),
                  transposeArray(thePPMImage.getGreenPixels()),
                  transposeArray(thePPMImage.getBluePixels())));
                  System.out.println(doubleMyRedPixels[4][6]);
  </code></pre>
  
  <p>It appears that the image becomes black once i hit this line.</p>
  
  <pre><code>thePPMImage=new PPMImage(doubleMyRedPixels, doubleMyGreenPixels, doubleMyBluePixels);
  System.out.println(doubleMyRedPixels[5][5]);
  </code></pre>
  
  <p>Ok so I've tried the following codes, <strong><em>separately</em></strong> in my nested for loop and I receive an ArrayOutOfBounds exception for each of them.My understanding of it is that if i have a pixel at (5,3) and I want to double it, I need it to be transposed and doubled in width and in height taking up 4 places. (10,6) upper left (10,7) upper right, (11,6) bottom left, (11,7) bottom right. so I need 4 assignment statements for each r g and b right? </p>
  
  <p>IDK
  I've tried the following  four as the contents of my nested for loop without any luck.</p>
  
  <pre><code>              //1st try
  
  
               doubleMyRedPixels[i][j]=gimmieRed[i/2][j/2];
               doubleMyGreenPixels[i ][j]=gimmieGreen[i/2][j/2];
               doubleMyBluePixels[i ][j]=gimmieBlue[i/2][j/2];
  
                //2nd try
                doubleMyRedPixels[i][j]=gimmieRed[i/2][j/2];
                doubleMyRedPixels[i][j]=gimmieRed[i / 2][(j / 2) - 1];
                doubleMyRedPixels[i][j]=gimmieRed[(i / 2) - 1][j /2];
                doubleMyRedPixels[i][j]=gimmieRed[(i / 2) - 1][(j /2) - 1];
  
                doubleMyGreenPixels[i ][j]=gimmieGreen[i/2][j/2];
                doubleMyGreenPixels[i][j]=gimmieGreen[i / 2][(j / 2) - 1];
                doubleMyGreenPixels[i][j]=gimmieGreen[(i / 2) -1][j / 2];
                doubleMyGreenPixels[i][j]=gimmieGreen[(i / 2) -1][(j / 2) - 1];
  
                doubleMyBluePixels[i ][j]=gimmieBlue[i/2][j/2];
                doubleMyBluePixels[i][j]=gimmieBlue[i /2][(j / 2) - 1];
                doubleMyBluePixels[i][j]=gimmieBlue[(i / 2) - 1][j / 2];
                doubleMyBluePixels[i][j]=gimmieBlue[(i / 2) -1][(j / 2) - 1];*/
                //3rd try
                doubleMyRedPixels[i ][j]=gimmieRed[i*2][j*2];
                doubleMyRedPixels[i][j]=gimmieRed[i * 2][(j * 2) + 1];
                doubleMyRedPixels[i][j]=gimmieRed[(i * 2) + 1][j * 2];
                doubleMyRedPixels[i][j]=gimmieRed[(i * 2) + 1][(j * 2) + 1];
  
                doubleMyGreenPixels[i ][j]=gimmieGreen[i*2][j*2];
                doubleMyGreenPixels[i][j]=gimmieGreen[i * 2][(j * 2) + 1];
                doubleMyGreenPixels[i][j]=gimmieGreen[(i * 2) + 1][j * 2];
                doubleMyGreenPixels[i][j]=gimmieGreen[(i * 2) + 1][(j * 2) + 1];
  
                doubleMyBluePixels[i ][j]=gimmieBlue[i*2][j*2];
                doubleMyBluePixels[i][j]=gimmieBlue[i * 2][(j * 2) + 1];
                doubleMyBluePixels[i][j]=gimmieBlue[(i * 2) + 1][j * 2];
                doubleMyBluePixels[i][j]=gimmieBlue[(i * 2) + 1][(j * 2) + 1];
  
                //4th try
                doubleMyRedPixels[i * 2][j * 2]=gimmieRed[i][j];
                doubleMyRedPixels[i * 2][(j * 2) + 1]= gimmieRed[i][j];
                doubleMyRedPixels[(i * 2) + 1][j * 2]= gimmieRed[i][j];
                doubleMyRedPixels[(i * 2) + 1][(j * 2) + 1]= gimmieRed[i][j];
  
                doubleMyGreenPixels[i * 2][j * 2]=gimmieGreen[i][j];
                doubleMyGreenPixels[i * 2][(j * 2) + 1]= gimmieGreen[i][j];
                doubleMyGreenPixels[(i * 2) + 1][j * 2]= gimmieGreen[i][j];
                doubleMyGreenPixels[(i * 2) + 1][(j * 2) + 1]= gimmieGreen[i][j];
  
                doubleMyBluePixels[i * 2][j * 2]=gimmieBlue[i][j];
                doubleMyBluePixels[i * 2][(j * 2) + 1]= gimmieBlue[i][j];
                doubleMyBluePixels[(i * 2) + 1][j * 2]= gimmieBlue[i][j];
                doubleMyBluePixels[(i * 2) + 1][(j * 2) + 1]= gimmieBlue[i][j];
  </code></pre>
  
  <p>Ok so I think I got it.</p>
  
  <p>My new 2D int arrays should have been initialized like this.</p>
  
  <pre><code>        int[][] doubleMyRedPixels = new int[gimmieRed.length * 2][gimmieRed[0].length * 2];
          int[][] doubleMyBluePixels = new int[gimmieBlue.length * 2][gimmieBlue [0].length * 2];
          int[][] doubleMyGreenPixels = new int[gimmieGreen.length * 2][gimmieGreen[0].length * 2];
  </code></pre>
  
  <p>and my for loops should be as follows;</p>
  
  <pre><code>    for (int i = 0; i &lt; longer; i++) {
          for (int j = 0; **j** &lt; wider; j++) {
  </code></pre>
  
  <p>and the aforementioned <strong>1st try</strong> allows me to double the size of any ppm image!</p>