0% found this document useful (0 votes)
94 views42 pages

Modifying Pictures Using Loops

The document discusses how pictures are encoded and stored as JPEG files. It explains that pictures are two-dimensional arrays of pixels, with each pixel represented by red, green, and blue color values. It also describes how to manipulate pictures in Java by accessing individual pixel color values and modifying them using methods like setRed() and setColor(). Loops can be used to efficiently modify multiple pixels at once.

Uploaded by

Dayom Ali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views42 pages

Modifying Pictures Using Loops

The document discusses how pictures are encoded and stored as JPEG files. It explains that pictures are two-dimensional arrays of pixels, with each pixel represented by red, green, and blue color values. It also describes how to manipulate pictures in Java by accessing individual pixel color values and modifying them using methods like setRed() and setColor(). Loops can be used to efficiently modify multiple pixels at once.

Uploaded by

Dayom Ali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Modifying Pictures using

Loops

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology
HOW PICTURES ARE ENCODED
Pictures (images, graphics) are an important part of any media
communication.
For our purposes, a picture is an image stored in a JPEG file. JPEG is an
international standard for how to store images with high quality but in little
space.

JPEG file (.jpg ) is:


An international standard
A lossy compression format:
 Lossy
means not all data is stored, but what is lost isn’t that important
 Compression means made smaller

Other formats for storing digital pictures are GIFF and BMP.
Pictures are two-dimensional arrays of pixels.

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 2
What is an Array?
• An array is a sequence of elements,
each with an index number associated
with it.
• You can access elements of an array in 0 1 2 3 4 5
Java using arrayName[index].
3 7 9 2 1 5
Example: To access the first element in an
array variable named pixels use pixels[0].

• You can get the number of items


in an array using arrayName.length
• To access the last element in the
array use:
pixels[arrayName.length - 1]
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 3
Two-imensional array (matrix)
Collection of elements arranged in both a horizontal and
vertical sequence.
For two-dimensional arrays we talk about an element at
column i and row j, that is, matrix[i][j].
0 1 2 3 4 5

0 3 7 9 2 1 5

1 2 5 4 8 3 6

2 8 7 4 1 9 2

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 4
Color representations
Our perception of light is limited by how our color sensors work. Our
eyes have sensors that trigger (peak) around 425 nanometers (blue),
550 nanometers (green), and 560 nanometers (red). Our brain
determines what a particular color based on the feedback from these
three sensors in our eyes.

The RGB color model


Based on how we perceive color, we can encode each pixel as a triplet
of numbers. The first number represents the amount of red in the
pixel. The second is the amount of green, and the third is the amount
of blue. We can make up any human-visible color by combining red,
green, and blue light. Combining all three gives us pure white. Turning
off all three gives us black. We call this the RGB color model.

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 5
The RGB color model
Based on how we perceive color, we
can encode each pixel as a triplet of
numbers. The first number represents
the amount of red in the pixel. The
second is the amount of green, and
the third is the amount of blue. We
can make up any human-visible color
by combining red, green, and blue
light. Combining all three gives us pure FIG. Merging red, green, and
white. Turning off all three gives us blue to make new colors
black. We call this the RGB color
model.
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 6
Other color models

The HSV color model which encodes Hue, Saturation,


and Value (sometimes also called the HSB color model for
Hue, Saturation, and Brightness).

the CMYK color model, which encodes Cyan, Magenta,


Yellow, and blacK (" B" could be confused with Blue). The
CMYK model is what printers. However, the four
elements means more to encode on a computer, so it's
less popular for media computation.

RGB is the most popular model on computers.


Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 7
 Each color component (sometimes called a channel) in a pixel is
typically represented with a single byte, eight bits. Eight bits can
represent 256 patterns (28): 0000000, 00000001, up through
11111111. We typically use these patterns to represent the values 0
to 255. Each pixel, then, uses 24 bits to represent colors. That means
that there are 224 possible patterns of 0's and 1's in those 24 bits.
That means that the standard encoding for color using the RGB
model can represent 16,777,216 colors.

 There are computer models that use more bits per pixel. For
example, there are 32 bit models which use the extra 8 bits to
represent transparency - how much of the color “below” the given
image should be blended with this color? These additional 8 bits are
sometimes called the alpha channel. There are other models that
actually use more than 8 bits for the red, green, and blue channels,
but they are uncommon.
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 8
FIGURE: RGB triplets in a matrix representation

TABLE: Number of bytes needed to store pixels


at various sizes and formats

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 9
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
pixel - by 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().
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 10
 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.
 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

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 11
We can get any particular pixel from a picture using getPixel(x,y) where x and y are the
coordinates of the pixel desired.
Pixel pixelObject = pictureObject.getPixel(0,0);
> System.out.println(pixelObject);
Pixel red=252 green=254 blue=251

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 column from top to bottom and
then all the pixels in the second column from top to bottom and so on till it has all of the pixels.
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

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 12
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).
> 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]

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 13
> import java.awt.Color;
> String fileName = "C:/intro-prog-
java/mediasources/caterpillar.jpg";
> Picture picture = new Picture(fileName);
> 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();

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 14
The Picture Explorer
 Tool that creates a copy of the current
picture and lets you explore it.
 The picture explorer will let you get pixel
information from a picture.
 display the x, y, red, green, and blue
values for a pixel, let you zoom in or out.
 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.
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 15
The Picture Explorer
 The picture explorer allows you to zoom
at various levels of magnification, by
choosing one in the Zoom menu.
 You can also type in the x and y values
and press 'Enter' to see the pixel
information for a particular pixel.

 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.)

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 16
Changing Color Values
The easiest thing to do with pictures is to change the color
values of their pixels by changing the red, green, and blue
components. Many of Adobe Photoshop's filters do just what
we're going to be doing in this section.

The way that we're going to be manipulating colors is by


computing a percentage of the original color. If we want 50% of
the amount of red in the picture, we're going to set the red
channel to 0.50 times whatever it is right now. If we want to
increase the red by 25%, we're going to set the red to 1.25 times
whatever it is right now. Recall that the asterisk (*) is the
operator for multiplication in Java.

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 17
1. Using a For-Each Loop
The syntax for a for-each loop is:
String fName = "C:/intro-prog- for (Type variableName : array)
java/mediasources/caterpillar.jpg"; First declare a variable that will be used in the body
Picture pict = new Picture(fName); of the loop, then for each element in the array
pict.show(); > Pixel[] pixelArray = pict.getPixels(); execute the body of the loop.
Pixel pixelObj = pixelArray[0];
int red = pixelObj.getRed(); public void decreaseRed()
{
red = (int) (red * 0.5);
Pixel[] pixelArray = this.getPixels();
pixelObj.setRed(red); int value = 0;
pixelObj = pixelArray[1]; // loop through all the pixels in the array
red = pixelObj.getRed(); for (Pixel pixelObj : pixelArray)
red = (int) (red * 0.5); {
pixelObj.setRed(red); // get the red value
value = pixelObj.getRed();
pixelObj = pixelArray[2];
red = pixelObj.getRed(); // decrease the red value by 50% (1/2)
red = (int) (red * 0.5); value = (int) (value * 0.5);
pixelObj.setRed(red);
pict.explore(); // set the red value of the current pixel to
the
new value
pixelObj.setRed(value);
}
Dr. Mounira Taileb King Abdulaziz University }
Faculty of Computing and Information Technology 18
Exercise
Write a method increaseRed() to loop through all
the pixels in a picture and double the red values
Multiply by 2
To try this method do the following:
String fileName = FileChooser.pickAFile();
Picture pictObj = new Picture(fileName);
pictObj.explore();
pictObj.increaseRed();
pictObj.explore();

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 19
2. Using While Loops
A while loop executes a statement (command) or group of statements in a block (inside open and
close curly braces). A while loop continues executing until a continuation test is false. When the
continuation test is false execution continues with the statement following the while loop.

Example: while loop that simply sets each pixel's color to black in a picture.
import java.awt.Color;
String fName = "C:/intro-prog-java/mediasources/caterpillar.jpg";
Picture pict = new Picture(fName);
pict.show();
Pixel[] pixelArray = pict.getPixels();
Pixel pixel = null;
int index = 0;
while (index < pixelArray.length)
{
pixel = pixelArray[index];
pixel.setColor(Color.black);
index++;
}
pict.repaint();
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 20
Let's talk through this code:

We will be using the Color class so we need to either use the fully qualified name (java.awt.Color) or
import the Color class using: import java.awt.Color;
Next we declare a variable with the name fileName to refer to the string object that has a particular
file name stored in it: C:/intro-prog-java/mediasources/caterpillar.jpg
The variable pict is created and refers to the new Picture object created from the picture
information in the file named by the variable fName.
We tell the Picture object to show (display) itself using pict.show();
Next we declare a variable pixelArray that references an array of Pixel objects (Pixel[]). We get the
array of Pixel objects by asking the Picture object for them using the getPixels() method.
We declare an object variable, Pixel pixel, that will refer to a pixel object but initialize it to null to
show that it isn't referring to any pixel object yet.
We declare a primitive variable index and initialize its value to 0.
Next we have the while loop. First we test if the value of index is less than the length of the array of
pixels with while (index < pixelArray.length). While it is, we set the variable pixel to refer to the pixel
object at the current value of index in the array of pixel objects. Next we set the color of that pixel to
the color black. Finally, we increment the variable index. Eventually the value of the variable index
will equal the length of the array of pixels and then execution will continue after the body of the
loop. Remember that in an array of five items the valid indexes are 04, so when the index is equal to
the length of the array you need to stop the loop.
The statement after the body of the while loop will ask the Picture object pict to repaint so that we
can see the color change.

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 21
3. Increasing/Decreasing Red (Green, Blue)
/**
* Method to decrease the red by half in the current
* picture
*/

public void decreaseRed()


{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int value = 0;
int index = 0;
// loop through all the pixels
while(index < pixelArray.length)
{
// get the current pixel
pixel = pixelArray[index];

// get the value


value = pixel.getRed();

// decrease the red value by 50% (1/2)


value = (int) (value * 0.5);

// set the red value of the current pixel to the new value
pixel.setRed(value);
// increment the index
index = index + 1;
}
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 22
 This program works on a Picture objectthe one that we'll use to get the pixels from. To create a
Picture object, we pass in the filename. After we ask the picture to decreaseRed(), we'll want to
repaint the picture to see the effect. Therefore, the decreaseRed method can be used like this:

String fName = "C:/intro-prog-java/mediasources/caterpillar.jpg";


Picture picture = new Picture(fName);
picture.show();
picture.decreaseRed();
picture.repaint();

Figure . The original picture (left) and red-decreased version (right).

you want to save your changes write them out to a file using the method
pictObj.write(String fileName); where pictObj is the name of the Picture
object and fileName is the full path name of the file.
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 23
Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 24
Increasing red
/**
* Method to increase the amount of red by 30%
*/

public void increaseRed()


{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int value = 0;
int index = 0;
// loop through all the pixels
while (index < pixelArray.length)
{
// get the current pixel
pixel = pixelArray[index];

// get the value


value = pixel.getRed();
Figure . Overly blue (left) and red increased by
30% (right).
// change the value to 1.3 times what it was
value = (int) (value * 1.3);

// set the red value to 1.3 times what it was


pixel.setRed(value);

// increment the index


index++;
}
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 25
Clear the Blue Component from a Picture
/**
* Method to clear the blue from the picture (set
* the blue to 0 for all pixels)
*/
public void clearBlue()
{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int index = 0;
// loop through all the pixels
while (index < pixelArray.length)
{
// get the current pixel Figure . Original (left) and blue erased (right).
pixel = pixelArray[index];

// set the blue on the pixel to 0


pixel.setBlue(0);

// increment index
index++;
}
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 26
4. Creating a Sunset
/**
* Method to simulate a sunset by decreasing the green
* and blue
*/
public void makeSunset()
{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int value = 0;
int i = 0;
// loop through all the pixels
while (i < pixelArray.length)
{
// get the current pixel
pixel = pixelArray[i];

// change the blue value


value = pixel.getBlue();
pixel.setBlue((int) (value * 0.7));

// change the green value


value = pixel.getGreen();
pixel.setGreen((int) (value * 0.7));
Figure . Original beach scene (left) and at (fake)
// increment the index
i++; sunset (right).
}
}

String fName = "C:/intro-prog-java/mediasources/beach-smaller.jpg";


Picture picture = new Picture(fName);
picture.explore();
picture.makeSunset();
picture.explore();

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 27
5. Making Sense of Methods
Homework
We want to write the methods to make them more general and reusable. We want our
methods to do one and only one thing, so that we can use the method again in a new
context where we need that one thing done. An example might make that clearer.
Consider the program to make a Sunset. That works by reducing the green and blue,
each by 30%. What if we rewrote that method so that it called two smaller methods.

/**
* Method to make a picture look like it was taken at sunset
* by reducing the blue and green to make it look more red
*/
public void makeSunset2()
{
decreaseGreen();
decreaseBlue();
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 28
Change all Pixel Colors by the Passed Amounts
public void changeColors(double redAmount, double greenAmount, double blueAmount)
{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int value = 0;
int i = 0;
// loop through all the pixels
while( i < pixelArray.length)
{
// get the current pixel
pixel = pixelArray[i];

// change the red value


value = pixel.getRed();
pixel.setRed((int) (redAmount * value));

// change the green value


value = pixel.getGreen();
pixel.setGreen((int) (greenAmount * value));

// change the blue value


value = pixel.getBlue();
pixel.setBlue((int) (blueAmount * value));

// increment i
i++;
}
}

String fName = "C:/intro-prog-java/mediasources/beach-smaller.jpg";


Picture picture = new Picture(fName);
picture.changeColors(1.0,0.7,0.7);
picture.show();

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 29
7. Using a For Loop
You may have had the problem that you forgot to declare the index variable
before you tried to use it in your while loop. You may also have had the
problem of forgetting to increment the index variable before the end of the
loop body. This happens often enough that another kind of loop is usually
used when you want to loop a set number of times. It is called a for loop.
/** * Method to clear the blue from the picture (set
* the blue to 0 for all pixels)
*/
public void clearBlue3()
{
Pixel[] pixelArray = this.getPixels();
// loop through all the pixels
for (int i=0; i < pixelArray.length; i++)
pixelArray[i].setBlue(0);
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 30
8. Lightening and Darkening
• Here's lightening and then darkening as methods.

/**
* Method to lighten the colors in the picture
*/
public void lighten()
{
Pixel[] pixelArray = this.getPixels();
Color color = null;
Pixel pixel = null;
// loop through all the pixels
for (int i = 0; i < pixelArray.length; i++)
{
// get the current pixel
pixel = pixelArray[i];
// get the current color
color = pixel.getColor();
// get a lighter color
color = color.brighter();
// set the pixel color to the lighter color
pixel.setColor(color);
}
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 31
/**
* Method to darken the color in the picture
*/
public void darken()
{
Pixel[] pixelArray = this.getPixels();
Color color = null;
Pixel pixel = null;
// loop through all the pixels
for (int i = 0; i < pixelArray.length; i++)
{
// get the current pixel
pixel = pixelArray[i];

// get the current color


color = pixel.getColor();

// get a darker color


color = color.darker();

// set the pixel color to the darker


color pixel.setColor(color);
}
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 32
9. Creating a Negative
Creating a negative image of a picture is /**
much easier than you might think at first. * Method to negate the picture
Let's think it through. What we want is the */ public void negate()
opposite of each of the current values for {
red, green, and blue. It's easiest to Pixel[] pixelArray = this.getPixels();
understand at the extremes. If we have a Pixel pixel = null;
int redValue, blueValue, greenValue = 0;
red component of 0, we want 255 instead.
// loop through all the pixels
If we have 255, we want the negative to for (int i = 0; i < pixelArray.length; i++)
have a zero. {
// get the current pixel
pixel = pixelArray[i];

// get the current red, green, and blue values


redValue = pixel.getRed();
greenValue = pixel.getGreen();
blueValue = pixel.getBlue();

// set the pixel's color to the new color


pixel.setColor(new Color(255 - redValue,
255 - greenValue,
255 - blueValue));
}
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 33
10. Converting to Grayscale
The resultant color is gray whenever the red component,
green component, and blue component have the same
value. That means that our RGB encoding supports 256
levels of gray from, (0, 0, 0) (black) to (1, 1, 1) through
(100, 100, 100) and finally (255, 255, 255). The tricky part
is figuring out what the replicated value should be.

What we want is a sense of the intensity of the color. It


turns out that it's pretty easy to compute: We average the
three component colors. Since there are three
components, the formula for intensity is:

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 34
Convert to Grayscale with More Careful Control of Luminance
/**
* Method to change the picture to grayscale with luminance
*/
public void grayscaleWithLuminance()
{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int luminance = 0;
double redValue = 0;
double greenValue = 0;
double blueValue = 0;
// loop through all the pixels
for (int i = 0; i < pixelArray.length; i++)
{
// get the current pixel
pixel = pixelArray[i];

// get the corrected red, green, and blue values


redValue = pixel.getRed() * 0.299;
greenValue = pixel.getGreen() * 0.587;
blueValue = pixel.getBlue() * 0.114;
Figure. Color picture converted
// compute the intensity of the pixel (average value)
luminance = (int) (redValue + greenValue + blueValue);
to grayscale.
// set the pixel color to the new color
pixel.setColor(new Color(luminance,luminance,luminance));
}
}

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 35
Concepts Summary: Arrays
Arrays are used to store many pieces of data of the same type. They allow you to
quickly access a particular item in the array using an index. If you couldn't use an
array, you would have to create a separate variable name for each piece of data.
To declare a variable that refers to an array, use the type followed by open '[' and
close ']' square brackets and then the variable name.
Pixel[] pixelArray;

This declares an array of Pixel objects. The value stored at each position in the
array is a reference to a Pixel object.
Arrays are objects, and you can find out how large an array is by getting its length.
pixelArray.length
Notice that this isn't a method call (there are no parentheses). This accesses a
public read-only field.
You can get an element of the array using arrayReference[index]. Where the index
values can range from 0 to arrayReference.length-1.
pixel = pixelArray[i];

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 36
Concepts Summary: Loops
We introduced three types of loops in this chapter: for-each, while and for.
The while loop has the keyword while followed by a boolean expression and then a
block of statements between an open and close curly brace. If the boolean expression
is true, the body of the loop will be executed. If the boolean expression is false,
execution will continue after the body of the loop (after the close curly brace). If you
just want to execute one statement in the body of the loop, then you don't need the
open and close curly braces, but you should indent the statement.

If you use a while loop to execute a block of statements a set number of times you will
need to declare a variable before the while and that variable will need to be changed in
the body of the loop. You may also need to declare other variables that you use in the
loop before the while. Don't declare variables inside the loop because you will use
more memory that way.

The for loop does the same thing as a while loop, but it lets you declare the variables
that you need for the loop, specify the boolean expression to test, and specify how to
change the loop variables all in one place. This means you are less likely to forget to do
each of these things.

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 37
Concepts Summary: Comments
Comments are text that the programmer adds to the code to explain the code. The compiler
ignores the comments when it translates the code into a form that the computer understands.
There are several types of comments in Java. To tell the compiler to ignore all text till the end
of the current line use //.
// get the current pixel
pixel = pixelArray[index];
To tell the compiler to ignore several lines use a starting /* and ending */.
/* set the red value to the original value
* times the passed amount
*/
pixel.setRed((int) (value * amount));

To put special comments in that can be parsed out by the javadoc utility to make html
documentation use a starting /** followed by an ending */.
/**
* Method to change the red by an amount
* @param amount the amount to change the red by
*/

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 38
Objects and Methods Summary
In this chapter, we talk about several kinds of encodings of
data (or objects).

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 39
Picture Methods

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 40
Pixel Methods

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 41
Color Methods

ColorChooser Methods

Dr. Mounira Taileb King Abdulaziz University Faculty of Computing and Information Technology 42

You might also like