How to pad an image in java?
<p>in order to padding an image, I am trying to pad a small 3*3 array. I could mirror original array's last column and last row to get a 4*4 array (my first code). But when I am trying this code for an 3*4 array I get ArrayIndexOutOfBound error (my second code). Can anyone help please? </p>

<p>-</p>

<h2>My first code:</h2>

<pre><code>package org.eclipse.wb.swing;

public class Padding_test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[][] table = new int[3][3];
        for (int row = 0; row &lt; 3; row ++)
            for (int col = 0; col &lt; 3; col++)
                table[row][col] = ((row * 3 + col)+1);

        for (int row = 0; row &lt; 3; row ++){
            for (int col = 0; col &lt; 3; col++){
                System.out.print (table[row][col]);
    }
          System.out.println();  
        }

        System.out.println();

        int [][] temp = new int[table.length+1][table[0].length+1] ;

        for ( int x=0 ; x&lt;table[0].length+1 ; x++){
            for (int y=0 ; y&lt;table.length+1 ; y++){
                temp[x][y] = 0;

                System.out.print(temp[x][y]);
            }
            System.out.println();
            }

        for ( int count = 0 ; count &lt; table.length ; count++){
        for (int xx=0 ; xx&lt;table[0].length ; xx++)
        {
            temp[xx][count]= table[count][xx];
            temp[xx+1][count]= temp[xx][count];
        }

        }

        int th = temp.length;

        System.out.println();

        for (int yy = 0 ; yy&lt; temp.length ; yy++){

            temp [yy][th-1]=temp [yy][th-2];            }


        for (int px =0 ; px&lt;temp.length ; px++){
            for (int py=0 ; py&lt;temp[0].length ; py++){
                System.out.print(temp[py][px]);
            }
            System.out.println();
        }

    }
}
</code></pre>

<p>_</p>

<h2>My Second code</h2>

<p>But I get error  when I run this code : </p>

<pre><code>package org.eclipse.wb.swing;

public class Padding_test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub      

        System.out.println();        
        int [][] table = { {1,2,3,4},{2,5,7,9},{6,8,9,0}};

        for (int i =0 ; i &lt;table.length ; i ++){
            for (int j =0 ; j &lt; table[0].length ; j++)
            {
                System.out.print(table[i][j]);
            }
            System.out.println();
        }

        System.out.println();
        int width = table[0].length;
        int Height = table.length;

        System.out.println( "Width = "+ width );
        System.out.println("Height = " + Height);    

        int [][] temp = new int[table.length+1][table[0].length+1] ;

        for ( int x=0 ; x&lt;table.length+1 ; x++){
            for (int y=0 ; y&lt;table[0].length+1 ; y++){
                temp[x][y] = 0;

                System.out.print(temp[x][y]);
            }
            System.out.println();
            }

        for ( int count = 0 ; count &lt; table.length ; count++){
            for (int xx=0 ; xx&lt;table[0].length ; xx++){
                temp[xx][count]= table[count][xx];
                temp[xx+1][count]= temp[xx][count];
            }       
        }

        int th = temp.length;

        System.out.println();

        for (int yy = 0 ; yy&lt; temp.length ; yy++){                              
            temp [yy][th-1]=temp [yy][th-2];            
        }       

        for (int px =0 ; px&lt;temp.length ; px++){
            for (int py=0 ; py&lt;temp[0].length ; py++){
                System.out.print(temp[py][px]);
            }
            System.out.println();
        }

    }
}
</code></pre>