title: Java BufferedImage: How do i copy an image properly n times by accessing the pixel - array?
body: <p>I am dealing with a problem which kind of &quot;forked&quot; from a project I'm working on. It was not necessary solving for the project itself, but I'm mentioning the origin because it is kind of a 'strangely specific' task.</p>
  <p>I am trying to read in a small BufferedImage from a file (8x8 pixels).
  This image's pixels are written into an integer array, with length 64 (obviously).</p>
  <p>Then, a new Array with the length 64*64 (=4096) is created. The pixels of the small array are copied into the large one 64 times, just resetting the smaller index to 0 everytime it reaches the end.</p>
  <p>Finally, I create a new BufferedImage with width = 64 and height = 64. The large array is then set as rgbArray of said BufferedImage. The code is as follows:</p>
  <pre><code>public static void main(String[] args)throws IOException {
      
      BufferedImage toCopy = ImageIO.read(new File(&quot;smallStripes.png&quot;));
      BufferedImage copiedNTimes = new BufferedImage(64, 64, BufferedImage.TYPE_BYTE_BINARY); 
      //copiedNTimes is to be the resulting image
  
      Graphics2D g2d = (Graphics2D) copiedNTimes.getGraphics();
      g2d.setColor(Color.WHITE);
      g2d.fillRect(0, 0, 64, 64);
  
      int[] smallPixels = new int[64];
  
      toCopy.getRGB(0, 0, 8, 8, smallPixels, 0, 8);
      //copy the rgb array of read image into the 64 - array
  
      int[] copied = copyNTimes(smallPixels, new int[64*64]);
  
      copiedNTimes.setRGB(0, 0, 64, 64, copied, 0, 8);
      //setting the rgb array of result image to the copied one
  
      FileOutputStream fos = new FileOutputStream(new File(&quot;result.png&quot;));
      ImageIO.write(copiedNTimes, &quot;png&quot;, fos);
  }
  
  static int[] copyNTimes(int[] small, int[] big){
  
      //this method copies the small array into the larger one
      //until the larger one is 'filled up'
  
      int index = 0;
  
      for(int x = 0 ; x &lt; big.length; x++){
          big[x] = small[index];
          index++;
          if(index == small.length)
              index = 0;
      }
  
      return big;
  }
  </code></pre>
  <p>It works more or less as I expected it to, but images are written 'shifted':</p>
  <p>smallStripes.png:</p>
  <p><a href="https://i.stack.imgur.com/lZgtP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lZgtP.png" alt="enter image description here" /></a></p>
  <p>result.png :</p>
  <p><a href="https://i.stack.imgur.com/z2HrD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z2HrD.png" alt="enter image description here" /></a></p>
  <p>My question is:</p>
  <p>How could I accomplish that the stripes 'line up' to each other? Right now it is, from left to right, 8px black, 8px white, 8px black... and so forth.
  Why not 64 px black (new line) 64 px white (new line) etc.?</p>
  <p>As is already said, it is oddly specific and oversimplified so I can describe it better.</p>