LAB 21 – Drawing (part 1 – Drawing using the Graphics Class)

Topics

n      To be able to draw simple shapes (lines, ovals, rectangles, arcs) on existing pictures

n      To draw text on a picture

n      To create pictures by drawing them

n      To promote reuse by working with existing classes

n      To use the Java API to explore classes and packages

 

7.1 Drawing using the Graphics Class

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 DrawOnPictures

{

  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 grid on a picture

   * @param pic - source picture

   */

  public static void drawGrid(Picture pic)

  {

    Pixel pix = null;

   

    //draw the horizontal lines

    for (int y = 0; y < pic.getHeight(); y+=20)

    {

        for (int x = 0; x < pic.getWidth(); x++)

        {

            pix = pic.getPixel(x,y);

            pix.setColor(Color.black);

        }

    }

    //draw the vertical lines

    for (int x = 0; x < pic.getWidth(); x+=20)

    {

        for (int y = 0; y < pic.getHeight(); y++)

        {

            pix = pic.getPixel(x,y);

            pix.setColor(Color.black);

        }

    }

  }

 

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

 

drawGrid(sourcePicture);

 

iii) Test your program with different pictures.

 

7.1.1 Drawing with Graphics Methods

 

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

 

   /* method to draw a filled box on a picture

   * @param pic - source picture

   * @param color - the color to draw the box with

   * @param topLeftX - the top left x coordinate of the box

   * @param topLeftY - the top left y coordinate of the box

   * @param width - the width of the box

   * @param height - the height of the box

   */

  public static void drawBox(Picture pic, Color color, int topLeftX,

                             int topLeftY, int width, int height)

  {

      // get the graphics context for drawing

      Graphics g = pic.getGraphics();

     

      //set the current color

      g.setColor(color);

   

      //draw the filled rectangle

      g.fillRect(topLeftX, topLeftY, width, height);

  }

 

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

 

    drawBox(sourcePicture, Color.green, x, y, width, height); where the variables have been declared and values have been assigned.

 

vi) Test your program with different pictures and different graphics (oval, filled oval, arcs).

 

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

 

  /* method to draw a string on a picture

   * @param pic - source picture

   * @param text - the string to draw

   * @param color - the color to draw the text with

   * @param x - starting location

   * @param y - baseline location

   */

  public static void drawString(Picture pic, String text, Color color, int x,

                             int y)

  {

      // get the graphics context for drawing

      Graphics g = pic.getGraphics();

     

      //set the current color

      g.setColor(color);

   

      // set the font

      g.setFont(new Font("Arial", Font.BOLD, 24));

      //draw the string

      g.drawString(text, x, y);

  }

 

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

 

        drawString(sourcePicture, "My World", Color.black,50, 200);

 

ix) Test your program with different pictures and different text, color, and fonts.

 

QUESTIONS:

Modify the program and methods to draw several figures and text on your picture using different colors and fonts.

 

Submit the .java file through the DropBox in WebCT.