import java.util.*; // Rotates an image 90 degree clockwise // public class Lect422c { public static void main(String[] args) { // get a picture to manipulate String file = FileChooser.pickAFile(); Picture pict = new Picture (file); System.out.println (pict.toString()); System.out.println ("Height: " + pict.getHeight()); System.out.println ("Width: " + pict.getWidth()); System.out.println ("Number of Pixels: " + (pict.getHeight() * pict.getWidth()) ); // displays the original picture pict.explore(); // modify the picture Picture newPict = rotate (pict); // display the picture in an interactive window new PictureExplorer (newPict); // save the picture to the computer //String file2 = FileChooser.pickAFile(); //System.out.println(file2); //newPict.write (file2); } // This code will rotate the picture 90 degree in a clockwise direction public static Picture rotate (Picture pict) { // declare variables for the loop int xIndex, xIndex2; int yIndex, yIndex2; Pixel pixel1, pixel2; // get width and height of picture int width = pict.getWidth(); int height = pict.getHeight(); // create the "empty canvas" on which the new picture will be drawn Picture pict2 = new Picture (height, width); // create a for loop to access/manipulate each pixel for ( xIndex = 0 ; xIndex < width ; xIndex = xIndex + 1 ) for ( yIndex = 0 ; yIndex < height ; yIndex = yIndex + 1 ) { // get the pixel from the original picture base on X,Y position pixel1 = pict.getPixel (xIndex, yIndex); // determine the corresponding pixel in the new picture xIndex2 = height - 1 - yIndex; yIndex2 = xIndex; pixel2 = pict2.getPixel (xIndex2, yIndex2); // draw the color from the original pixel to the new pixel java.awt.Color colorValue; colorValue = pixel1.getColor(); pixel2.setColor(colorValue); } return pict2; } }