CSCI 121 Programming Assignment 1

 

Implement the PhotoFlop™ application as described below.  When executed from the command line (java PhotoFlop) a GUI interface resembling the following should appear:

 

 

The bulk of this document describes the functionality of each of the buttons.  First of all, you should understand that you will be implementing 2 Java classes:  MyPicture and PhotoFlop.  MyPicture will provide most of the functionality of the application.  PhotoFlop provides the user interface.

 

Open Picture

 

When the user clicks the Open Picture button, PhotoFlop should display a file chooser dialog that allows the user to select a file.  The file name selected by the user should be used to create a MyPicture object and cause it to show itself.  Actually, you should display a copy of the MyPicture object.  You accomplish this by using a copy constructor.  In the MyPicture class, enter the following code:

  public MyPicture(MyPicture p)

  {

    super(p);

  }

 

In the actionPerformed method of PhotoFlop, do something like this (picture and copyPicture are MyPicture instance variables of the class PhotoFlop):

 

  picture = new MyPicture(fileName);

  copyPicture = new MyPicture(picture);

  copyPicture.show();

 

Subsequent transformations to the picture should be done to copyPicture.  Assume we have opened and displayed the picture below.  Transformations will be done to this picture.

 

 

Vertical Flip

 

When the user clicks the Vertical Flip button, the image should be flipped vertically.  Code for this was discussed in class and lab.  You probably already have this method implemented in your MyPicture class definition.  For the example picture, this produces:

 

 

Horizontal Flip

When the user clicks the Horizontal Flip button, the image should be flipped horizontally.  Code for this was discussed in class and lab.  You probably already have this method implemented in your MyPicture class definition.  For the example picture, this produces:

 

           

Rotate Right

 

When the user clicks the Rotate Right button, the image should be rotated 90o clockwise. We will discuss how to do this in class.  The result for the example picture is:

 

           

 

Rotate Left

 

When the user clicks the Rotate Left button, the image should be rotated 90o counterclockwise. We will discuss how to do this in class.  The result for the example picture is:

 

 

Grayscale

 

When the user clicks the Grayscale button, the image should be displayed in grayscale. We did this in lab.  The result for the example picture is:

 

 

Lighter and Darker

 

The Lighter and Darker buttons cause a lighter or darker version of the image to be displayed.  This effect can be accomplished by getting the color of each pixel in the picture and then using the Color class’s brighten() and darken() methods.  The results for the example photo are:

 

 

Undo

 

When the user presses the Undo button,  copyPicture should be reinitialized with the original picture.

 

Save

 

When the user presses the Save button, a JFileChooser object should be used to display a File Save dialog that allows the user to enter a file name under which to save the transformed copyPicture.  Here is code to do this:

 

   JFileChooser fc = new JFileChooser();

   int returnValue = fc.showSaveDialog(null);

   if (returnValue == JFileChooser.APPROVE_OPTION)

   {

     copyPic.write(fc.getSelectedFile().getPath());

   }

 

General approach to MyPicture:

 

/** include a comment with your name  !!!!!! */

 

public class MyPicture extends Picture
{

   /* constructors */

   public MyPicture(String fileName)

   {

      super(fileName);

   }

  

    /* other methods to implement flip, rotate, grayscale,

       lighten, darken, etc. */

}

 

General approach to PhotoFlop:

 

/** include a comment with your name  !!!!!! */

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class PhotoFlop extends JFrame

   implements ActionListener
{

   private MyPicture picture, copyPicture;

   /* other instance variables */

 

   /* constructor */

   public PhotoFlop()

   {

 

      /* code to layout and initialize GUI */

   }

 

   public void actionPerformed(ActionEvent e)

   {
      /* code to handle button clicks */
   }

 

   public static void main(String[] args)

   {

      PhotoFlop pf = new PhotoFlop();

      pf.setVisible(true);

   }

}

 

What to Hand In

 

I would like for you to email (to mclark@wvutech.edu) an electronic of the .java files necessary to compile and execute your program.  This will probably be 2 files:  PhotoFlop.java and MyPicture.java.  Do not send copies of the book classes.  The subject of the email should be “CS I Program 1”.  Be sure to clearly identify yourself in the text of the email.  Include the .java files as attachments.

 

I also want you to hand in printed listings of the files MyPicture.java and PhotoFlop.java.  These should be given to me in class on the due date of the assignment.