title: Java Combine multiple images into a single bigger image without overlapping
body: <p>I'm trying to combine several images into a larger image using Java. The images that are passed in are all height 127 x width 293. The idea is that a number of images are passed to the method and the method takes the images and builds them into another larger image. There is going to be a layout for the larger image where a total of 12 possible images can be input to the larger image, spaced out evenly (2 rows of 6 images, none overlapping). If there are fewer than 12 images passed in, then only the first however many spaces will be filled, the rest of the image will be white because the background is to going to be white. When I run the program it prints the larger image, but it will only fill the first space showing the first image in the upper left, regardless of how many images are passed in. Also the background is a pinkish color instead of the intended white background. I'm only a beginner with Java so I'm trying to work through some of these learning pains. Any advice on how I might be able to solve my problem? (Code is copied below for reference) Thanks!</p>
  
  <pre><code>public class ImagesCombine {
  
  public String BuildImgs (File[] imgs)throws IOException {
      int arsize = imgs.length;
      File path = new File("Z:/JAVAFiles/Images/");
      BufferedImage page = new BufferedImage(620,900,BufferedImage.TYPE_INT_ARGB);
      Graphics2D paint;
      paint = page.createGraphics();
      paint.setPaint(Color.WHITE);
      paint.fillRect ( 0, 0, page.getWidth(), page.getHeight() ); 
      paint.setBackground(Color.WHITE);
      String tmpname = "";
  
      for (int i=0;i&lt;imgs.length;i++){
  
          if(i==0){ 
              Image img0 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img0,0,0,null);
              paint.dispose();
              }
          if(i==1){
              Image img1 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img1,323,0,null);
              paint.dispose();
              }
          if(i==2){
              Image img2 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img2,0,142,null);
              paint.dispose();
              }
          if(i==3){
              BufferedImage img3 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img3,323,142,null);
              paint.dispose();
              }
          if(i==4){
              BufferedImage img4 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img4,0,284,null);
              paint.dispose();
              }
          if(i==5){
              BufferedImage img5 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img5,323,284,null);
              paint.dispose();
              }
          if(i==6){
              BufferedImage img6 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img6,0,426,null);
              paint.dispose();
              }
          if(i==7){
              BufferedImage img7 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img7,323,426,null);
              paint.dispose();
              }
          if(i==8){
              BufferedImage img8 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img8,0,568,null);
              paint.dispose();
              }
          if(i==9){
              BufferedImage img9 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img9,323,568,null);
              paint.dispose();
              }
          if(i==10){
              BufferedImage img10 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img10,0,710,null);
              paint.dispose();
              }
          if(i==11){
              BufferedImage img11 = ImageIO.read(new File(path, imgs[i].getName()));
              paint.drawImage(img11,323,710,null);
              paint.dispose();
              }
  
          }
      String outpath = "Z:\\JAVAFiles\\" + imgs[0].getName().substring(0,16) + ".jpg";
  
      OutputStream outfile = new FileOutputStream(outpath);
  
      JPEGImageEncoder encoder2 = JPEGCodec.createJPEGEncoder(outfile);
      encoder2.encode(page);
      outfile.close();
      return("Success");  
  }
  }
  </code></pre>