title: How to Stich to Image objects in Java
body: <p>I have a scenario in which i`m getting a number of tiles (e.g.12) from my mapping server. Now for buffering and offline functions I need to join them all back again so that we have to deal with 1 single image object instead of 12. I ve tried to do it without JAI my code is below.</p>
  
  <pre><code>package imagemerge;
  
  
  import java.awt.*;
  import java.awt.image.*;
  import java.awt.event.*;
  
  public class ImageSticher extends WindowAdapter {
  
      Image tile1;
      Image tile2;
      Image result;
      ColorModel colorModel;
      int width,height,widthr,heightr;
      //int t1,t2;
      int t12[];
  
  
      public ImageSticher()
      {
  
      }
  
      public ImageSticher (Image img1,Image img2,int w,int h)
      {
          tile1=img1;
          tile2=img2;
  
          width=w;
          height=h;
          colorModel=ColorModel.getRGBdefault();
  
      }
  
      public Image horizontalStich() throws Exception
      {
  
          widthr=width*2;
          heightr=height;
  
          t12=new int[widthr *  heightr];
  
          int t1[]=new int[width*height];
          PixelGrabber p1 =new PixelGrabber(tile1, 0, 0, width, height, t1, 0, width);
          p1.grabPixels();
  
          int t2[]=new int[width*height];
          PixelGrabber p2 =new PixelGrabber(tile2, 0, 0, width, height, t1, 0, width);
          p2.grabPixels();
  
          int y, x, rp, rpi;
          int red1, red2, redr;
          int green1, green2, greenr;
          int blue1, blue2, bluer;
          int alpha1, alpha2, alphar;
  
          for(y=0;y&lt;heightr;y++)
          {
              for(x=0;x&lt;widthr;x++)
              {
                  //System.out.println(x);
                  rpi=y*widthr+x; // index of resulting pixel;
                  rp=0;           //initializing resulting pixel
                  System.out.println(rpi);
  
                  if(x&lt;(widthr/2)) // x is less than width , copy first tile
                  {
                      //System.out.println("tile1="+x);
                      blue1 = t1[rpi] &amp; 0x00ff; // ERROR occurs here
                      green1=(t1[rpi] &gt;&gt; 8) &amp; 0x00ff;
                      red1=(t1[rpi] &gt;&gt; 16) &amp; 0x00ff;
                      alpha1 = (t1[rpi] &gt;&gt; 24) &amp; 0x00ff;
  
                      redr = (int)(red1 * 1.0); // copying red band pixel into redresult,,,,1.0 is the alpha valye
                      redr = (redr &lt; 0)?(0):((redr&gt;255)?(255):(redr));
                      greenr = (int)(green1 * 1.0); //
                      redr = (int)(red1 * 1.0); //
                      greenr = (greenr &lt; 0)?(0):((greenr&gt;255)?(255):(greenr));
                      bluer = (int)(blue1 * 1.0);
                      bluer =  (bluer &lt; 0)?(0):((bluer&gt;255)?(255):(bluer));
                      alphar = 255;
  
                      //resulting pixel computed
                      rp = (((((alphar &lt;&lt; 8) + (redr &amp; 0x0ff)) &lt;&lt; 8) + (greenr &amp; 0x0ff)) &lt;&lt; 8) + (bluer &amp; 0x0ff);
  
  
                  }
                  else // index is ahead of half way...copy second tile
                  {
  
                      blue2 = t2[rpi] &amp; 0x00ff; // blue band bit of first tile
                      green2=(t2[rpi] &gt;&gt; 8) &amp; 0x00ff;
                      red2=(t2[rpi] &gt;&gt; 16) &amp; 0x00ff;
                      alpha2 = (t2[rpi] &gt;&gt; 24) &amp; 0x00ff;
  
                      redr = (int)(red2 * 1.0); // copying red band pixel into redresult,,,,1.0 is the alpha valye
                      redr = (redr &lt; 0)?(0):((redr&gt;255)?(255):(redr));
                      greenr = (int)(green2 * 1.0); //
                      redr = (int)(red2 * 1.0); //
                      greenr = (greenr &lt; 0)?(0):((greenr&gt;255)?(255):(greenr));
                      bluer = (int)(blue2 * 1.0);
                      bluer =  (bluer &lt; 0)?(0):((bluer&gt;255)?(255):(bluer));
                      alphar = 255;
  
                      //resulting pixel computed
                      rp = (((((alphar &lt;&lt; 8) + (redr &amp; 0x0ff)) &lt;&lt; 8) + (greenr &amp; 0x0ff)) &lt;&lt; 8) + (bluer &amp; 0x0ff);
  
  
                  }
                  t12[rpi] = rp; // copying resulting pixel in the result int array which will be converted to image
  
              }
          }
  
          MemoryImageSource mis;
          if (t12!=null) {
              mis = new MemoryImageSource(widthr, heightr, colorModel, t12, 0, widthr);
              result = Toolkit.getDefaultToolkit().createImage(mis);
              return result;
  
          }
          return null;
  
      }
  
  
  
  
  
  }
  </code></pre>
  
  <p>now to check the my theory I<code>m trying to join or stich two tiles horizontaly but i</code>m getting the error :</p>
  
  <p>java.lang.ArrayIndexOutOfBoundsException: 90000
      at imagemerge.ImageSticher.horizontalStich(ImageSticher.java:69)
      at imageStream.ImageStream.getImage(ImageStream.java:75)
      at imageStream.ImageStream.main(ImageStream.java:28)</p>
  
  <p>is there some kind of limitation because when stiching two images of 300 x 300 horizontally it means the resulting image will be 600 x 300 ... that would make 180000 index size but its giving error at 90000, what am I doing wrong here</p>