<< Chapter < Page | Chapter >> Page > |
The explore method
The call to the explore method is new to this module.
The explore method is defined in the SimplePicture class, which is the superclass of the Picture class. The method is inherited into the Picture class.
javadocs description of the explore method
The javadocs description of this method is shown in Figure 4 .
Figure 4 . javadocs description of the explore method. |
---|
Method to open a picture explorer on a copy of this simple picture. |
Result of calling the explore method
The result of calling the explore method in Listing 3 is to create and display the PictureExplorer object shown in Figure 1 .
Very important capability
The availability of the explore method and the PictureExplorer class is very important in at least two respects:
Implementing the algorithm
The code in Listing 4 implements the algorithm required to modify the original image to make it look like the image shown in Figure 2 .
Listing 4 . Implementing the algorithm. |
---|
Pixel[] pixelArray = pic.getPixels();for(Pixel pixel:pixelArray ){
pixel.setRed(255 - pixel.getRed());pixel.setGreen(255 - pixel.getGreen());
pixel.setBlue(0);}//end for loop |
In particular, the code in Listing 4 sets the blue color components to 0 and inverts the red and green color componentsfor every pixel in the picture.
One of several approaches
There are several ways to do this, and this is only one of those ways. This approach makes use of a method named getPixels that is defined in the SimplePicture class and inherited into the Picture class.
Very useful when...
This approach is particularly useful when you want to perform the same action on every pixel in an image. The advantage is that you don't have to worryabout horizontal and vertical coordinates with this approach. Access to all of the pixels is provided in a one-dimensional array.
javadocs description of the getPixels method
The javadocs description of this method is shown in Figure 5 .
Figure 5 . javadocs description of the getPixels method. |
---|
Method to get a one-dimensional array of
Pixels for this simple picture.Returns: a one-dimensional array of Pixelobjects starting with y=0 to y=height-1
and x=0 to x=width-1. |
What is a Pixel object?
An object of Ericson's Pixel class encapsulates an individual pixel from an image. Figure 6 shows the javadocs description of the Pixel class.
Figure 6 . javadocs description of the Pixel class. |
---|
Class that references a pixel in a picture.
A pixel has an x and y location in a picture.A pixel knows how to get and set the red, green,
blue, and alpha values in the picture.A pixel also knows how to get and set the color
using a Color object. |
Many methods available
The Pixel class defines a large number of methods. Once you have a reference to a Pixel object, you can manipulate the underlying pixel encapsulated in that object in a variety of ways.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?