CS116      Lab 5 - Drawing

Objectives:

  1. Before starting this lab you should have read chapter 7 in your text.
     
  2. Open DrJava and the Picture class.
     
  3. Let's start out by having some fun with the Graphics class. Do you have a picture of yourself or a friend?
     

    Assignment:
    Write a program which draws glasses on a person who does not normally wear glasses.  This program will only work for this particular picture!

    Add a caption horizontally centered on the bottom of your picture using the drawString method.
    Hint: Look at code on p226 for centering text


     

  4. More advanced drawing techniques can be achieved with the Graphics2D class.  The Graphics2D class is a subclass of the Graphics class -- it inherits everything from the Graphics class plus has more stuff.  Look at the documentation for that class and feel free to try out a few methods.  Check out the method clipToEllipse on p 244 in your text.
     

  5.  

    Assignment:
    Did you struggle with the clipping a triangular shape in lab3?  Your cruel professor wanted you to have practice with loops in a matrix and hid from you the fact that these tasks can be done easily with the Graphics2D class!  Write a method which clips an image in the shape of a triangle. 

    Hints:  A triangle is a type of Polygon with three sides.  To create a polygon you need to provide two arrays for the x coordinates and y coordinates of the three points of the triangle.  You can create an array like this:
      int [] xpoints = {0,10,5};
    This is just and example.  You will need to supply the appropriate values in the array using the width and height of the picture.


     

  6. Assignment:
    Write a method which rotates a picture by an amount specified by a parameter. 

    Hints:  Get the AffineTransform from the Graphics2D object and then use the rotate method on the transform.  You will want to rotate the picture around the center point of the picture.  Then set the transform and draw the image.


     

  7. Send your modified code and resulting picture to me using GoucherLearn.