Lab Assignment 9

Fall Semester, 2006

 

Outcomes:

 

Upon completion of the lab, the student will know how to

 

·        Use JLabel and BufferedImage to manipulate image files

 

·        Use the Java Stack class to reverse a sequence of actions

 

1

Download the following file to your J: drive from Mr. Clark’s website:

·        Lab9.java

·        3.bmp

 

2

Compile and execute Lab9.  When the FileChooser dialog appears, select the picture 3.bmp.  You should see the picture appear in a frame with 2 buttons below it.  Neither button works at this point.

 

The picture is actually split among a grid of JLabels, where each JLabel was created using a subimage of the picture (similar to the MosaicPicture application described in lecture). 

 

We will modify this application so that

·        the Scramble button will swap the icons of 2 randomly selected JLabels.  The Random class will be used to generate these values.

·        the Undo button will undo the most recent swap. Successive clicks of the Undo button will undo the sequence of swaps caused by pressing the Scramble button.  A stack will help us accomplish this.

 

3

Make the scramble() method work as described in the comment in its body. 

·        Use the Random object rand, which was created by the Lab9 constructor, to create two random points, each representing a column and row of a label on the grid that was created by the constructor. Recall that rand.nextInt(n) will return an integer i in the range 0 £ i < n.

·        The column number is constrained to be in the range
0
£ column_number < gridWidth

·        The row number is constrained to be in the range
0
£ row_number < gridHeight

 

·        Once you have created the two Point objects, you will then

·        push each Point object on the stack created by the constructor

·        pass the Point objects to swap() to make the exchange. You must therefore also implement the swap() method to see any change in the picture.

 

When you get this part working, demonstrate your program to the lab assistant/instructor.

 

4

Make the undo() method work as described in the comment in its body. 

 

·        If the stack is not empty,

·        pop the Point p1 from the stack

·        pop the Point p2 from the stack

·        pass p1 and p2 to swap() to undo the most recent scramble

 

When you get this part working, demonstrate your program to the lab assistant/instructor.

 

5

Print a listing of your code and get the lab assistant/instructor to sign it.  Hand this in for lab credit.