Section 4.2. Manipulating Pictures


[Page 85 (continued)]

4.2. Manipulating Pictures

We manipulate a picture in DrJava by making a picture object out of a JPEG file, then changing the pixels in that picture. We change the pixels by changing the color associated with the pixelby manipulating the red, green, and blue components.

We make a picture using new Picture(fileName). We make the picture appear with the method show(). We can also explore a picture with the method explore(). These are both object methods so they must be called on an object of the class that understands the method. This means that show() and explore() must be called on a Picture object (object of the Picture class) using dot notation as in pictureObject.show().

> String fileName = FileChooser.pickAFile(); > System.out.println(fileName); c:\intro-prog-java\mediasources\caterpillar.jpg > Picture pictureObject = new Picture(fileName); > pictureObject.show(); > System.out.println(pictureObject); Picture, filename c:\intro-prog-java\mediasources\caterpillar.jpg height 150 width 329



[Page 86]

What new Picture(fileName) does is to scoop up all the bytes in the input filename, bring them in to memory, reformat them slightly, and place a sign on them "This is a picture object!" When you execute Picture pictureObject = new Picture(fileName), you are saying "The name pictureObject is referring to a Picture object created from the contents of the file."

Picture objects know their width and their height. You can query them with the methods getWidth() and getHeight().

> System.out.println(pictureObject.getWidth()); 329 > System.out.println(pictureObject.getHeight()); 150


We can get any particular pixel from a picture using getPixel(x,y) where x and y are the coordinates of the pixel desired. This returns an object of the class Pixel which knows the picture it is from and the x and y position of the pixel in that picture. The x coordinate starts at 0 at the top left of the picture and increases horizontally. The y coordinate starts at 0 at the top left of the picture and increases vertically. We can also get a one-dimensional array containing all the pixels in the picture using the method getPixels(). This just grabs all the pixels in the first row from left to right followed by all of the pixels in the second row from left to right and so on till it has all of the pixels.

> Pixel pixelObject = pictureObject.getPixel(0,0); > System.out.println(pixelObject); Pixel red=252 green=254 blue=251 > Pixel[] pixelArray=pictureObject.getPixels(); > System.out.println(pixelArray[0]); Pixel red=252 green=254 blue=251


Pixels know where they came from. You can ask them their x and y coordinates with getX() and getY().

> System.out.println(pixelObject.getX()); 0 > System.out.println(pixelObject.getY()); 0


Each pixel object knows how to get the red value getRed() and set the red value setRed(redValue). (Green and blue work similarly.)

> System.out.println(pixelObject.getRed()); 252 > pixelObject.setRed(0); > System.out.println(pixelObject.getRed()); 0


You can ask a pixel object for its color with getColor(), and you can ask the pixel object to set the color with setColor(color). Color objects (objects of the class Color in package java.awt) know their red, green, and blue components. You can also create new Color objects with


[Page 87]

new Color(redValue,greenValue,blueValue)


(the color values must be between 0 and 255). The Color class also has several colors predefined that you can use. If you need a color object that represents the color black you can use Color.black or Color.BLACK, for yellow use Color.yellow or Color.YELLOW. Other colors that are predefined are: Color.blue, Color.green, Color.red, Color.gray, Color.orange, Color.pink, Color.cyan, Color.magenta, and Color.white (or use all capitals for the color names). Notice that this is accessing fields on the Color class, not invoking class methods (no parentheses). Public class variables (fields) can be accessed using ClassName.fieldName.

Making it Work Tip: Importing Classes from Packages

Color is a Java class in the package java.awt. A package is a group of related classes. Java uses packages to group classes that you need for a particular purpose. To use classes in packages other than java.lang (which contains System and Math), you will need to import them. Importing a class or all classes in a package allows you to use the name of a class without fully qualifying it. To fully qualify a name, use the package name followed by a period (dot) and the class name. The fully qualified name for the Color class is java.awt.Color. You can always use the fully qualified name instead of importing, but people don't usually want to type that much. To import all classes in the package java.awt use import java.awt.*;. To import just the Colorclass from the package java.awt use import java.awt.Color;. Importing doesn't make your class larger, it is just used to determine what class you mean.


Debugging Tip: Undefined Class Error

If you get the message "Error: Undefined class Color'' it means that you didn't import the class Color. You must either import classes that are in packages other than java.lang or fully qualify them.


> import java.awt.Color; > Color colorObj=pixelObject.getColor(); > System.out.println(colorObj); java.awt.Color[r=0,g=254,b=251] > Color newColorObj=new Color(0,100,0); > System.out.println(newColorObj); java.awt.Color[r=0,g=100,b=0] > pixelObject.setColor(newColorObj); > System.out.println(pixelObject.getColor()); java.awt.Color[r=0,g=100,b=0]


If you change the color of a pixel, the picture that the pixel is from does get changed. However, you won't see the change until the picture repaints.

> System.out.println(pictureObject.getPixel(0,0)); Pixel red=0 green=100 blue=0



[Page 88]

Common Bug: Not Seeing Changes in the Picture

If you show your picture, and then change the pixels, you might be wondering, "Where are the changes?!?'' Picture displays don't automatically update. If you ask the Picture object to repaint using pictureObject.repaint(), the display of the Picture object will update. Asking the picture to show itself again pictureObject.show() will also repaint it.


You can automatically get a darker or lighter color from a Color object with colorObj.darker() or colorObj.brighter(). (Remember that this was easy in HSV, but not so easy in RGB. These methods do it for you.)

> Color testColorObj = new Color(168,131,105); > System.out.println(testColorObj); java.awt.Color[r=168,g=131,b=105] > testColorObj = testColorObj.darker(); > System.out.println(testColorObj); java.awt.Color[r=117,g=91,b=73] > testColorObj = testColorObj.brighter(); > System.out.println(testColorObj); java.awt.Color[r=167,g=130,b=104]


Notice that even though we darken the color and then brighten it the final color doesn't exactly match the original color. This is due to rounding errors. A rounding error is when calculations are done in floating point but the answer is stored in an integer. The floating point result can't fit in the type of the result (integer) and so some of the detail is lost.

You can also get a color using ColorChooser.pickAColor(), which gives you a variety of ways of picking a color. ColorChooser is a class that we have created to make it easy for you to pick colors using the Java class javax.swing.JColorChooser.

> import java.awt.Color; > Color pickedColorObj = ColorChooser.pickAColor(); > System.out.println(pickedColorObj); java.awt.Color[r=51,g=255,b=102]


When you have finished manipulating a picture, you can write it out to a file with write(fileName).

> pictureObject.write("newPicture.jpg");


Common Bug: End filenames with .jpg

Be sure to end your filename with ".jpg" in order to get your operating system to recognize it as a JPEG file.



[Page 89]

Common Bug: Saving a File Quicklyand How to Find it Again!

What if you don't know the whole path to a directory of your choosing? You don't have to specify anything more than the base name. The problem is finding the file again! In what directory did it get saved? This is a pretty simple bug to resolve. The default directory (the one you get if you don't specify a path) is wherever DrJava is.


We don't have to write new methods to manipulate pictures. We can do it from the command area using the methods (functions) just described. Please reset the interactions pane by clicking the RESET button at the top of DrJava before you do the following.

> import java.awt.Color; > String fName = "C:/intro-prog-java/mediasources/caterpillar.jpg"; > Picture picture = new Picture(fName); > picture.show(); > picture.getPixel(10,100).setColor(Color.black); > picture.getPixel(11,100).setColor(Color.black); > picture.getPixel(12,100).setColor(Color.black); > picture.getPixel(13,100).setColor(Color.black); > picture.getPixel(14,100).setColor(Color.black); > picture.getPixel(15,100).setColor(Color.black); > picture.getPixel(16,100).setColor(Color.black); > picture.getPixel(17,100).setColor(Color.black); > picture.getPixel(18,100).setColor(Color.black); > picture.getPixel(19,100).setColor(Color.black); > picture.repaint(); > picture.explore();


Making it Work Tip: Reuse the Previous Line in DrJava

You can use the up arrow on the keyboard to bring up previous lines you have typed in the interactions pane in DrJava. You can then use the left arrow key to get to a character to correct or change and then execute it by pressing the 'Enter' key.


The result showing a small black line on the left side below the middle of the leaf appears in Figure 4.13. The black line is 100 pixels down, and the pixels 10 through 19 from the left edge have been turned black.

Figure 4.13. Directly modifying the pixel colors via commands: Note the small black line on the left under the line across the leaf.
(This item is displayed on page 90 in the print version)


4.2.1. Exploring Pictures

On your CD, you will find the MediaTools application with documentation for how to get it started. You can also open a picture explorer in DrJava. Both the MediaTools application and the picture explorer will let you get pixel information from a picture. You can see the picture explorer in Figure 4.14 and the MediaTools application appears in Figure 4.15. Both of these will display the x, y, red, green, and blue values for a pixel. They will also both let you zoom in or out.

Figure 4.14. Exploring the caterpillar with the line.
(This item is displayed on page 90 in the print version)


Figure 4.15. Using the MediaTools image exploration tools on barbara.jpg.
(This item is displayed on page 91 in the print version)


The picture explorer can be opened on a Picture object. Picture p = new Picture(FileChooser.pickAFile()); will allow you to define a Picture object and name it p. You can open a picture explorer on the picture using p.explore(). The picture explorer will make a copy of the current picture and show it. The copy will not be affected by any changes you make to the picture.


[Page 90]

The picture explorer allows you to zoom at various levels of magnification, by choosing one in the Zoom menu. As you move your cursor around in the picture, press down with the mouse button. You'll be shown the (x, y) (horizontal, vertical) coordinates of the pixel your mouse cursor is currently over, and the red, green, and blue values at that pixel. You can use the next and previous buttons to change the pixel that you want to examine. You can also type in the x and y values and press 'Enter' to see the pixel information for a particular pixel.

The MediaTools application works from files on the disk. If you want to check out a file before loading it into DrJava, use the MediaTools application. Click on the Picture Tools box in MediaTools, and the tools will open. Use the Open button to bring up a file selection boxyou click on directories you want to explore on the left, and images you want on the right, then click OK. When the image appears, you have several different tools available. Move your cursor over the picture and press down with the mouse button.


    [Page 91]
  • The red, green, and blue values will be displayed for the pixel you're pointing at. This is useful when you want to get a sense of how the colors in your picture map to numeric red, green, and blue values. It's also helpful if you're going to be doing some computation on the pixels and want to check the values.

  • The x and y values will be displayed for the pixel you're pointing at. This is useful when you want to figure out regions of the screen, e.g., if you want to process only part of the picture. If you know the range of x and y coordinates where you want to process, you can tune your program to reach just those sections.

  • Finally, a magnifier is available to let you see the pixels magnified. (The magnifier can be clicked and dragged around.)



Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net