Showing posts with label Rotate an image in JAVA. Show all posts
Showing posts with label Rotate an image in JAVA. Show all posts

Tuesday, May 8, 2012

Rotate an image in JAVA

//write one program that will rotate an image 180 degrees in a clockwise direction (or counter clockwise
//direction since both give the same result) and one program that will rotate an image 90 degrees in a
//counter-clockwise direction.








public class Lab10a
{

public static void main (String[] args)
{

System.out.println("bilal abuzenah");

// Access and open the picture
String filename = FileChooser.pickAFile ();
Picture p = new Picture (filename);

// call the method to modify the Pictture
Picture p2;
p2 = modifyPicture (p);

// explore (display) the picture
//p2.explore();
p2.show();
String saveFilename = FileChooser.pickAFile ();
p2.write (saveFilename);
// get a new filename and save the picture
//String saveFilename = FileChooser.pickAFile ();
//p2.write (saveFilename);

} // end of main

public static Picture modifyPicture (Picture p)
{
// get the width and height of the picture
int width = p.getWidth();
int height = p.getHeight();
//System.out.println ("The picture is " + width + " pixels wide and " + height + " pixel high.");

System.out.println ("Width: " + width + ", Height: " + height);

// create the new Picture that will be returned
Picture resultPicture;
resultPicture = new Picture (width, height);

// Set up a loop to access all the X position
int xPos;
int yPos;
int xNew;
int yNew;
Pixel pix;
Pixel pix2;
int red;
int green;
int blue;

for ( xPos = 0 ; xPos < width ; ++xPos )
{
for ( yPos = 0 ; yPos < height ; ++yPos )
{
// access the pixel to be modifed
pix = p.getPixel (xPos, yPos);

xNew = (width - 1) - xPos;
yNew = (height - 1) - yPos;
pix2 = resultPicture.getPixel (xNew, yNew);



// get the colr values from the original pixel
red = pix.getRed();
green = pix.getGreen();
blue = pix.getBlue();

// set those color values into the result pixel
pix2.setRed(red);
pix2.setGreen(green);
pix2.setBlue(blue);

//System.out.println ("The picture is " + xNew + " pixels wide and " + yNew + " pixel high.");


}
}

return resultPicture;
} // end of modifyPicture

} // end of class