LAB 22 – Drawing (part 2 – Advanced Drawing using the Graphics Class)

Topics

n      To use the Java2D API for more complicated drawing.

n      To promote reuse by working with existing classes

n      To use the Java API to explore classes and packages

 

7.2 Programs As Specifying Drawing Process

Type in the following code in the Program window (changing the comments):

 

import java.awt.*;

/* Program that draws on existing pictures

 *using the Graphic Class

 *

* Picture and Pixel classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Oct. 3, 2007

 */

 

public class DrawPictures

{

  public static void main(String[] args)

  {

    String fileName1;

    System.out.println(“Find the source file”);

    fileName1 = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

    Picture sourcePicture;

    sourcePicture = new Picture(fileName1);

 

 // calls to methods that manipulate images go here

 

   sourcePicture.show(); 

  }

}

 

i) Type in the following code in the Program window below the main method and before the last }:

 

  /* method to draw a picture with a succession of filled rectangles

   * with the top left corner the darkest and the bottom right the lightest

   * @param pic - source picture

   */

  public static void drawFilledRectangles(Picture pic)

{

    Graphics g = pic.getGraphics();

    Color color = null;

   

    // loop 25 times

    for (int i = 25; i > 0; i--)

    {

      color = new Color(i * 10, i * 5, i);

      g.setColor(color);

      g.fillRect(0,0,i*10,i*10);

    }

}

 

ii) Type in the following code in the Program window in the main method and before the pic.show();

 

drawFilledRectangles(sourcePicture);

 

iii) Test your program with different pictures and different constants in the method.

 

7.3 Using Graphics2D for Advanced Drawing

7.3.2 Copying Pictures by Drawing Images

 

iv) Type in the following code in the Program window below the main method and before the last }:

 

   /* method to copy the source picture on a target picture

   * at the given location

   * @param source - source picture

   * @param target - target picture

   * @param x - the top left x coordinate to copy to

   * @param y - the top left y coordinate to copy to

   */

  public static void copy(Picture source, Picture target, int x, int y)

  {

    // get the graphics object

    Graphics g = target.getGraphics();

    Image img = source.getImage();

   

    // copy the image

    g.drawImage(img,x,y,null);

  }

 

v) Type in the following code in the Program window in the main method and before the pic.show(); [be sure to use FileChooser to get the targetPicture and show it instead of the source picture.]

 

    copy(sourcePicture, targetPicture, 200, 300);

 

vi) Test your program with different pictures.

 

vii) Type in the following code in the Program window below the main method and before the last }:

 

   /* method to clip the source picture to an ellipse

   * @param source - source picture

   * @return a new picture with the image clipped to an ellipse

   */

  public static Picture clipToEllipse(Picture source)

 {

    int width = source.getWidth();

    int height = source.getHeight();

    Picture result = new Picture(width,height);

   

    // get the graphics2D object

    Graphics g = result.getGraphics();

    Graphics2D g2 = (Graphics2D) g;

 

    // create an ellipse for clipping

    Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,width,height);

   

    // use the ellipse for clipping

    g2.setClip(ellipse);

   

    // draw the image

    g2.drawImage(source.getImage(), 0,0,width, height,null);

   

    // return the result

    return result;

  }

 

viii) Type in the following code in the Program window in the main method and before the pic.show(); [be sure to declare targetPicture and to import java.awt.Graphics2D and java.awt.geom.*

 

        targetPicture = clipToEllipse(sourcePicture);

 

ix) Test your program with different pictures.

 

QUESTIONS:

Modify the program and methods to clip a picture to an Ellipse (or RoundRectangle) and then copy a second smaller image on the picture.

 

Submit the .java file through the DropBox in WebCT.