CSCI 201 Homework 6

This assignment is a minor modification of Homework 5. It gives you a chance to either (1) almost double your credit for Homework 5 or (2) fix minor errors in Homework 5.

There are three changes you'll be asked to do for this assignment:

  1. Make your program look pretty.
  2. Modify your program so that it read command line arguments.
  3. At a third blend method that uses the Graphics2D class.


This homework assignment (and the following two) will be given twice the weight as previous homework assignments in computing the overall homework average.

Starting out

Start out with a good Homework 5 solution. If you didn't do Homework 5, you need to figure it out now. Rename your HW5 class to be H6 and store in the

Making it pretty

Write your program in such a way that its logical structure is clearly seen. This means that statments within an if-statement should be neatly aligned. For examples, look at the programs on the section schedule page. One very good, and brief, presentation of a coding standing for writing programs that look good is Sun's recommended code conventions for Java.

Reading command line arguments

Grading Homework 5 will be difficult because there is no way to change the image file without recompiling the code. We need to fix this.

The main method of your present Homework 5 starts with two lines similar to the following.

    String p1Name = "blahblahblah" ;
    String p2Name = "blahblahblah" ;

Right after these two lines add the following if statement that will allow the names of the two image files to be changed when your program is run.

    if (args.length >= 2) {
      p1Name = args[0] ;
      p2Name = args[1] ;

You can "exercise" this if by going to DrJava's Interactions pane and typing something like the following command.

java HW6 "NameOfFirstImageFile" "NameOfSecondImageFile" 

You can also try typing this directly from the command line on Windows, Mac OS, and Linux, but that requires specifying a class path.


Now add a third method for combining the pictures. Start out with something like the following:

  public static Picture graphicsBlend(Picture p1, Picture p2) {
    Picture pn = new Picture(p1.getWidth(),p2.getHeight()) ;
    for (int x=0; x<p1.getWidth(); ++x) {
      for (int y=0; y<p1.getHeight(); ++y) {
        pn.getPixel(x,y).setColor(p1.getPixel(x,y).getColor()) ;
    Graphics2D g = (Graphics2D) pn.getGraphics().create() ;
    // Add two or more statements here
    return pn ;

The nested for loop in this code is to copy p1 to pn. You should be able to do this in one line with the new Picture constructor, but alas there appears to be a bug in DrJava.

Now add two or more statements to creating a clipping area for your Graphics2D object g and then use the drawImage method of g to copy part of the second picture into the new picture, which is itself a copy of the first picture. You may need to do some reading of Section 7.3 to accomplish this.

Due date and turning it in

This assignment is due by class on November 14. Submit your assignment using the UNCA Moodle to the assignment labeled "Homework 6 -- Combining images even more".