package com.naughtyzombie.graphicsdemo;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
 * Created by pram on 17/04/2015.
 */

public class RotateDemo {
    public static void main(String[] args) throws Exception {
        new RotateDemo();
    }

    public RotateDemo() throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame editorFrame = new JFrame("Image Demo");
                editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                BufferedImage image = null;
                try {
                    image = ImageIO.read(RotateDemo.class.getResource("/london.jpg"));
                    int type = image.getType();

                    int[][] imgMatrix = new int[image.getHeight()][image.getWidth()];

                    int rows = imgMatrix.length;
                    int columns = imgMatrix[0].length;

                    for (int i=0; i < rows; i++) {
                        for (int j=0; j < columns; j++) {
                            imgMatrix[i][j] = image.getRGB(j,i);
                        }
                    }

                    imgMatrix = rotateCW(imgMatrix);

                    rows = imgMatrix.length;
                    columns = imgMatrix[0].length;
                    image = new BufferedImage(columns,rows,type);

                    for (int i=0; i < rows; i++) {
                        for (int j=0; j < columns; j++) {
                            image.setRGB(j,i, imgMatrix[i][j]);
                        }
                    }



                    // The required drawing location
                    /*int drawLocationX = 300;
                    int drawLocationY = 300;*/

                    // Rotation information
                    /*double rotationRequired = Math.toRadians(45);
                    double locationX = image.getWidth() / 2;
                    double locationY = image.getHeight() / 2;
                    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
                    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);*/

                    // Drawing the rotated image at the required drawing locations
                    //g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null);

                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
                ImageIcon imageIcon = new ImageIcon(image);
                JLabel jLabel = new JLabel();
                jLabel.setIcon(imageIcon);
                editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);

                editorFrame.pack();
                editorFrame.setLocationRelativeTo(null);
                editorFrame.setVisible(true);
            }
        });
    }

    public static void rotate(int[][] matrix) {
        int n = matrix.length;
        if (n <= 1) {
            return; // nothing to do
        }

        /* layers */
        for (int i = 0; i < n / 2; i++) {
            /* elements */
            for (int j = i; j < n - i - 1; j++) {
                int saved = matrix[i][j];
                matrix[i][j] = matrix[n - j - 1][i];
                matrix[n - j - 1][i] = matrix[n - 1 - i][n - 1 - j];
                matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
                matrix[j][n - 1 - i] = saved;
            }
        }
    }

    public static void rotate2(int[][] matrix) {
        transpose(matrix);
        reflect(matrix);
    }

    public static void reflect(int[][] arr) {
        int size = arr.length;
        for (int k = 0; k < size; k++) {
            for (int i = 0; i < Math.floor(size / 2); i++) {
                swap(arr, k, i, k, size - i - 1);
            }
        }
    }

    public static void transpose(int[][] arr) {
        int size = arr.length;
        for (int diag = 0; diag < size; diag++) {
            for (int i = diag + 1; i < size; i++) {
                swap(arr, diag, i, i, diag);
            }
        }
    }

    public static void swap(int[][] arr, int row1, int col1, int row2, int col2) {
        int num1 = arr[row1][col1];
        int num2 = arr[row2][col2];
        arr[row1][col1] = num2;
        arr[row2][col2] = num1;
    }

    public int[][] rotateCW(int[][] mat) {
        final int M = mat.length;
        final int N = mat[0].length;
        int[][] ret = new int[N][M];
        for (int r = 0; r < M; r++) {
            for (int c = 0; c < N; c++) {
                ret[c][M-1-r] = mat[r][c];
            }
        }
        return ret;
    }
}

@@ -73,69 +73,13 @@
 awt.
-geom.AffineTransform;%0Aimport java.awt.image.*;%0Aimport java.io
+image
 .*;%0A
@@ -1220,18 +1220,16 @@
 = rotate
-CW
 (imgMatr
@@ -1622,2810 +1622,569 @@
   %7D%0A
-%0A%0A%0A                    // The required drawing location%0A                    /*int drawLocationX = 300;%0A                    int drawLocationY = 300;*/%0A%0A                    // Rotation information%0A                    /*double rotationRequired = Math.toRadians(45);%0A                    double locationX = image.getWidth() / 2;%0A                    double locationY = image.getHeight() / 2;%0A                    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);%0A                    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);*/%0A%0A                    // Drawing the rotated image at the required drawing locations%0A                    //g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null);%0A%0A                %7D catch (Exception e) %7B%0A                    e.printStackTrace();%0A                    System.exit(1);%0A                %7D%0A                ImageIcon imageIcon = new ImageIcon(image);%0A                JLabel jLabel = new JLabel();%0A                jLabel.setIcon(imageIcon);%0A                editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);%0A%0A                editorFrame.pack();%0A                editorFrame.setLocationRelativeTo(null);%0A                editorFrame.setVisible(true);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public static void rotate(int%5B%5D%5B%5D matrix) %7B%0A        int n = matrix.length;%0A        if (n %3C= 1) %7B%0A            return; // nothing to do%0A        %7D%0A%0A        /* layers */%0A        for (int i = 0; i %3C n / 2; i++) %7B%0A            /* elements */%0A            for (int j = i; j %3C n - i - 1; j++) %7B%0A                int saved = matrix%5Bi%5D%5Bj%5D;%0A                matrix%5Bi%5D%5Bj%5D = matrix%5Bn - j - 1%5D%5Bi%5D;%0A                matrix%5Bn - j - 1%5D%5Bi%5D = matrix%5Bn - 1 - i%5D%5Bn - 1 - j%5D;%0A                matrix%5Bn - 1 - i%5D%5Bn - 1 - j%5D = matrix%5Bj%5D%5Bn - 1 - i%5D;%0A                matrix%5Bj%5D%5Bn - 1 - i%5D = saved;%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static void rotate2(int%5B%5D%5B%5D matrix) %7B%0A        transpose(matrix);%0A        reflect(matrix);%0A    %7D%0A%0A    public static void reflect(int%5B%5D%5B%5D arr) %7B%0A        int size = arr.length;%0A        for (int k = 0; k %3C size; k++) %7B%0A            for (int i = 0; i %3C Math.floor(size / 2); i++) %7B%0A                swap(arr, k, i, k, size - i - 1);%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static void transpose(int%5B%5D%5B%5D arr) %7B%0A        int size = arr.length;%0A        for (int diag = 0; diag %3C size; diag++) %7B%0A            for (int i = diag + 1; i %3C size; i++) %7B%0A                swap(arr, diag, i, i, diag);%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static void swap(int%5B%5D%5B%5D arr, int row1, int col1, int row2, int col2) %7B%0A        int num1 = arr%5Brow1%5D%5Bcol1%5D;%0A        int num2 = arr%5Brow2%5D%5Bcol2%5D;%0A        arr%5Brow1%5D%5Bcol1%5D = num2;%0A        arr%5Brow2%5D%5Bcol2%5D = num1;%0A    %7D%0A%0A    public int%5B%5D%5B%5D rotateCW
+                %7D catch (Exception e) %7B%0A                    e.printStackTrace();%0A                    System.exit(1);%0A                %7D%0A                ImageIcon imageIcon = new ImageIcon(image);%0A                JLabel jLabel = new JLabel();%0A                jLabel.setIcon(imageIcon);%0A                editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);%0A%0A                editorFrame.pack();%0A                editorFrame.setLocationRelativeTo(null);%0A                editorFrame.setVisible(true);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public int%5B%5D%5B%5D rotate
 (int

tidy up