rotate an image 90 degrees

0

I have a code problem with this method rotate 90Right ().

Statement Write a method that is capable of rotating an image 90 degrees to the right.

This is my code:

/**
* Rota la imagen 90 grados hacia la derecha
*/
public void rotar90ALaDerecha()
{
    Color temp[][]= new Color[ALTO_MAXIMO][ANCHO_MAXIMO];
    for(int i=0; i<alto;i++)
      {
        for(int j=0; j<ancho;j++)
        {
            temp[j][alto-1-i]=bitmap[i][j];
        }
      }
      bitmap = new Color[ANCHO_MAXIMO][ALTO_MAXIMO];
      bitmap = temp;
}

I get this error:

The height of the image does not change, when rotating it should be changed by the original width. It was expected 300, but you answered 200 expected:

Note: here I leave the class diagram and thank you very much for your help in advance.

link

    
asked by Jhon James Hernandez 08.02.2018 в 16:19
source

1 answer

1
public void rotar90ALaDerecha() {
    int ancho = bitmap[0].length;
    int alto = bitmap.length;

    Color temp[][]= new Color[ancho][alto];
    int nuevaColumna = alto - 1;
    for(int i = 0; i < alto; i++, nuevaColumna--) {
        for(int j = 0; j < ancho; j++) {
            temp[j][nuevaColumna] = bitmap[i][j];
        }
    }
    bitmap = temp;
}

This bitmap = new Color[ANCHO_MAXIMO][ALTO_MAXIMO]; is not necessary since what you are doing is reserving memory space for a new matrix and you are putting bitmap to that matrix and on the line below you change the reference of bitmap and you put it to point to temp . For this reason you are reserving unnecessary memory space.

Example of operation:

    
answered by 08.02.2018 / 22:26
source