












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













In this week’s lab, you will construct a program that implements two digital image processing tasks:
do. Finally, the image processing operations we want you to implement will be controlled by GUI com- ponents known as sliders, so we will also introduce the JSlider class in this lab. Step 0: Getting Started While working on this lab, you will need some image files to use as samples. We have provided a folder of such images as a .zip file which you can download from the course web page. To make it possible for
tory that contains your BlueJ project directory. So, e.g., if in BlueJ you create a new project called Lab7, and the Lab7 folder is on the Desktop, then you should place (and unzip) the picture file in your home directory. Step 1: Accessing and Displaying an Image As a start, let’s construct a program that displays just one of the three buttons that will appear in your final program’s control window, the “Load Image” button.
Step 2: Using a JFileChooser Now that you can display the Lunch.gif file, we would like to make your program flexible enough to be able to display any image file. Swing provides a class named JFileChooser that makes it fairly easy to do this.
Run the program again after making this change to ensure that it handles the cancel button correctly. Step 3: Slippery Sliders In the image on the bottom of the first page, we showed a window containing a GUI component we have not previously used, the JSlider. A JSlider is a component that provides a simple way for a user to graphically select a value by sliding a knob within a bounded interval. The slider in the example window on the first page of this handout allows the program’s user to determine how many shades of gray will be used to display an image. To introduce you to sliders, we would like you to implement a program that uses a slider in a somewhat less sophisticated way. The interface for the program we want you to write is shown on the right. Its purpose is simply to let a user select a number between 0 and 100 using a JSlider and to display the number currently selected. The number selected is determined by the relative position of the slider’s knob. In the figure on the right, the knob is about one third of the way from the left end of the slider, so the value displayed is 33, one third of 100. To begin, use the “New Class..” button to create a new class that extends GUIManager. While we want you to write this as a standalone program, by the end of the lab we will incorporate its code as a subcomponent of the image processing program described at the begin- ning of this handout. This subcomponent will handle the “Adjust Levels” button. Accordingly, you should name your new class AdjustLevels. In addition to the usual import statements for javax.swing and squint, you should add an import statement for java.awt.*. As usual, start by writing a constructor that displays the basic elements of the program’s interface without actu- ally making them work. This program displays four components.
bers are used to describe brighter pixels. The brightness numbers are ultimately encoded in binary using 8 bits. so the smallest number is 0 (black) and the largest is 255 (white) for 2^8 = 256 colors total. A dark gray pixel might have a brightness value of 80 and a light gray pixel’s brightness might be 190. For example, if you look closely enough at a picture of an eye (as shown below): you will discover that it is really composed of distinct squares of varying shades of gray: Within the computer, each of these gray squares is represented by a single number describing its bright- ness. The table below, shows all of the numeric values that would be used to describe the image of the eye. 233 232 223 210 198 202 214 219 226 229 233 240 248 248 247 241 238 242 239 218 201 173 143 120 122 131 148 173 194 204 212 224 242 246 242 239 239 235 218 172 137 106 83 81 78 91 114 143 177 190 190 208 229 234 232 233 233 234 188 142 110 99 100 120 148 162 167 174 188 189 192 196 219 226 229 233 242 213 166 123 115 116 115 141 151 158 160 173 185 192 189 201 219 229 231 245 218 172 120 112 112 113 97 97 98 111 124 147 184 189 182 210 228 232 247 223 165 134 146 129 126 109 113 91 120 118 104 136 172 175 204 225 232 246 226 186 161 139 155 197 141 108 131 204 173 89 80 130 153 165 215 230 246 231 215 196 194 183 173 178 189 199 201 198 168 125 107 139 147 187 222 248 238 233 220 215 204 183 168 163 164 174 188 205 205 175 168 175 191 217 250 239 233 231 231 230 221 213 205 188 181 193 211 219 221 222 221 219 224 250 241 230 231 231 239 234 228 230 230 228 231 231 234 238 239 238 232 227 Earlier, we saw that we could construct an SImage by providing a file name to the SImage constructor. You can also construct an SImage by providing a collection of numbers describing the brightness of the pixels of the desired image. In general, this can be difficult to do since many values may be required to describe even a relatively small image. There is, however, one special case where it is quite easy to do. This is the case where all of the pixels should have the same brightness. To illustrate the process of creating such an image, we would like you to modify your AdjustLevels program so that it creates and displays a monotone image whose brightness is controlled by the slider dis- played at the bottom of its window. A snapshot of what the program’s window might look like is shown on the left.
First, make sure your window size is (300, 175), and change the range associated with the JSlider from 0-100 to 0-255. Next, in the sliderChanged method, after the code that sets the text of the JLabel, construct an SImage specifying the current setting of the JSlider as the brightness value. This can be done using a construction of the form new SImage( width, height, brightness ) Make the SImage’s width be 256 and its height be 100. Associate the SImage with a local variable name. Then, use the setIcon method of the JLabel that appears in the center of your AdjustLevels window to display in your program’s window the new SImage, whose bright- ness is based on the slider value. (The SImage will not appear until you have moved the slider. We will change this later.) Compile and run your program. Test it by moving the slider back and forth. Step 5: Thinking Inside the Box We would like you to draw a frame around the gray images you create as shown on the right. This will require a four step process.
will take an existing SImage associated with the name somePic, and create a new image that is identical to the original except that it has a black line along its left edge. The first line simply associates an array containing the brightness values for the original image with the name pixels. The loop then sets each of the brightness values associated with the pixels with x coordinate 0 (i.e., the left edge of the image) to 0 (i.e., black). This does not actually modify the image since the array produced by getPixelArray is a copy of the brightness values. The last statement creates a new image with the modified brightness values and makes somePic refer to this new image.
the rectangle. To learn a little more about manipulating pixel values, let’s make an image that contains every possible shade of gray. In particular, we want you to modify your makePixelArray method so that it will create a pixel array that describes a gradient like that shown in the window to the right. The image drawn in this window contains all 256 possible shades of gray. The left edge of the rectangle is solid black. The right edge is white. Each of the lines between these two extremes is drawn with a color one shade lighter than the line to its left. (For now, we will just ignore the position of the slider. That is, initially your program should draw the same gradient no matter how the slider is set.) If you think about how we just described the gradient, you will realize that every pixel should have a brightness value equal to its x coordinate. The pixels on the left edge have x coordinate 0 and they are supposed to be black which corresponds to a brightness value of 0. The pixels on the right edge are sup- posed to be white. This is described by the brightness value 255 which happens to be the x coordinate of all of the pixels on the right edge (since earlier we cleverly told you to make the SImage 256 pixels wide). This means that you can set each pixel to the correct color by executing a statement like: pixels[x][y] = x; for all possible values of x and y. To execute this statement for all possible x and y values you will use two for loops. This time, however, instead of coming one after another, one of the for loops should be nested inside of the other. One of the loops will start with loop index x at 0, and increment x by one on each iteration of the loop until the value of x reaches the width of the image. The other loop will step through all values of the y coordinate from 0 to the height of the image. The inner loop will sit within the outer loop and the statement that sets array ele- ments equal to x will be placed within the inner loop. These nested loops should replace the loops that draw the frame. Therefore, while you may want to keep the loops that draw the frame as examples while you write the loops that produce the gradient, once the code to draw the gradient is complete you should delete the loops that draw the frame.
The SImage class provides methods named getPixelArray, getWidth and getHeight. You should use these methods to determine the three additional parameters passed from your sliderChanged method to your adjustPixelArray method..
We wanted only 5 bands, but instead we get 6, due to roundoff errror. We can’t simply fix the problem by converting to double values and using arithmetic on double values here, since we are using the specific properties of integer division (e.g., that x/51 = 0 if x is an integer in the range of 0 to 50) to determine which pixels fall into which band. Instead, we need to use an if statement to “eliminate” the extra band caused by roundoff. So, to handle this, you should:
Submission Instructions As usual, submit your completed BlueJ lab projects by dropping them into Professor Lawson’s Netfiles Inbox. Grading Completeness (14 points) / Correctness (6 points)
- File dialog displayed when Load button pressed - Images displayed correctly in ImageViewer - ImageViewer buttons create appropriate processing win- dows. - AdjustLevels reduces brightness of images - Quantizer correctly requantizes images Style (10 points) - Commenting - Good variable names - Good, consistent formatting - Correct use of instance vari- ables and local variables - Good use of blank lines - Uses names for constants Appendix: Summary of New Library Features Used in this Lab Accessing or Creating an Image: new SImage( someString )
SImage.GREEN, and SImage.BLUE are associated with the values 0, 1, and 2 respectively so that one can say things like someSImage.getPixMap( SImage.RED ) Displaying images in JLabels someJLabel.setIcon( someSImage );