title: stitch images together in java
body: <p>I'm trying to stitch some images together using java.  I have a bunch of images I'd like to stitch together and they are all the same dimensions so it's really just a question of lining them up next to each other I suppose.  I have it working but it's very slow and probably very memory intensive.  I'm wondering if there's an easier way:</p>
  
  <pre><code>public static void main(String[] args) throws IOException
      {
          int dim = 256;
          BufferedImage merged = null;
          for(int y = 0; y&lt;10;y++)
          {
              for(int x = 0; x&lt;10;x++)
              {
                  URL url = new URL(someURL);
                  BufferedImage nextImage = ImageIO.read(url);
                  if(merged==null)
                      merged=nextImage;
                  else
                  {
                      BufferedImage tempMerged;
                      tempMerged = new BufferedImage(10*dim,10*dim,merged.getType());
                      //Write first image
                      for(int xx=0;xx&lt;merged.getWidth();xx++)
                          for(int yy=0;yy&lt;merged.getHeight();yy++)
                              tempMerged.setRGB(xx,yy,merged.getRGB(xx,yy));
                      //Write img2
                      for(int xx=0;xx&lt;dim;xx++)
                      {
                          for(int yy=0;yy&lt;dim;yy++)
                          {
                              int destX = (x*dim)+xx;
                              int destY = (y*dim)+yy;
                              tempMerged.setRGB(destX,destY,nextImage.getRGB(xx,yy));
                          }
                      }
                      merged=tempMerged;
                  }
                  System.out.println("Stitched image at "+x+","+y);
              }
          }
          ImageIO.write(merged, "png", new File("merged.png"));
      }
  </code></pre>