/** * Copy one image to create a new image * * @author Pat Troy: troy AT uic DOT edu */ // This example has the turtle draw in a picture that can be saved. import java.util.*; import java.awt.*; public class Lect1016b { public static void main(String[] args) { // create the picture and a turtle String fileName = FileChooser.pickAFile(); Picture pict = new Picture (fileName); // get an array of pixels Pixel pixArray[]; pixArray = pict.getPixels(); System.out.println ("Width: " + pict.getWidth()); System.out.println ("Height: " + pict.getHeight()); System.out.println ("Total Pixels: " + pict.getWidth() * pict.getHeight()); System.out.println ("Array Length: " + pixArray.length ); System.out.println ( pixArray [0] ); // create the duplicate "canvas" Picture pict2 = new Picture (pict.getWidth(), pict.getHeight()); int index; Pixel pix, pix2; Color cl1, cl2; int red, green, blue, gray; int xPos, yPos; for ( index = 0 ; index < pixArray.length ; index++ ) { //get the pixel from the source image pix = pixArray[index]; // get the x, y coordinates for the source pixel xPos = pix.getX(); yPos = pix.getY(); // get the destination pixel using the x,y coordinates pix2 = pict2.getPixel (xPos, yPos); // retrieve the source color information red = pix.getRed(); green = pix.getGreen(); blue = pix.getBlue(); // store the color information in the destination pixel pix2.setRed (red); pix2.setGreen (green); pix2.setBlue (blue); } // display the picture pict.show(); pict2.show(); // save the picture to the computer // Note: Be sure to add the .jpg to the end of the filename!!!!! fileName = FileChooser.pickAFile(); System.out.println(fileName); pict2.write (fileName); } }