Rotate a matrix by 90 degrees - in Java
<p>I need to write a method that rotates a matrix (of objects) by 90 degrees, both directions.</p>

<p>for example - </p>

<pre><code>( 0,0,0) (0,0,0) (0,0,0) (0,0,0)

( 1,1,1) (1,1,1) (1,1,1) (1,1,1)

( 2,2,2) (2,2,2) (2,2,2) (2,2,2)
</code></pre>

<p>becomes - </p>

<pre><code>(2,2,2) (1,1,1) (0,0,0)

(2,2,2) (1,1,1) (0,0,0)

(2,2,2) (1,1,1) (0,0,0)

(2,2,2) (1,1,1) (0,0,0) 
</code></pre>

<p>I can't get over the "ArrayIndexOutOfBoundsException" error.</p>

<p>This is my work so far -</p>

<pre><code>private RGBColor [][] _picture;
private int _row;
private int _col;

public RGBImage(RGBColor[][] pixels)
{
    _picture = new RGBColor [pixels.length][pixels[0].length];
    _row = pixels.length;
    _col = pixels[0].length;

    for(int i = 0 ; i &lt; pixels.length ; i++)
    {
        for(int j = 0 ; j &lt; pixels[0].length ; j++)
        {
            _picture[i][j] = pixels[i][j];
        }
    }
}

public double[][] toGrayscaleArray ( )
{
    double [][] copyToGrayscale = new double [_row][_col];
    for(int i = 0 ; i &lt; _row ; i++)
        {
            for(int j = 0 ; j &lt; _col ; j++)
            {
               copyToGrayscale [i][j] = _picture[i][j].convertToGrayscale();
            }
        }
        return copyToGrayscale;
}

public void rotateCounterClockwise ( )
{
    RGBImage copy = new RGBImage(_col,_row);
    for(int i=0 ; i &lt; _col ; i++)
    {
        for(int j=_row-1 ; j &gt;0 ; j--)
        {
            copy._picture[i][j]=_picture[j][i];
        }
    } 
    _picture = copy.toRGBColorArray();     
}
</code></pre>

<p>What should i do?
thanks.</p>