To gain more understanding of objects and methods in Java using the Picture class.
To gain an understanding of Picture objects and Pixel objects.
Go over the notes: topic 4.
The exercises in this lab as well as in future labs use images provided by the textbook authors. These images may be downloaded as an archive, called mediaSources.
Download the mediaSources zip file here.
Uncompress this file (for instance, double click on the filename: this will start up the WinZip utility). This will extract the media files into a folder called intro-prog-java/mediasources.
There are many files containing images in the mediaSources folder. This exercise shows you how to pick a file from the mediaSources folder and display the image stored in it on the screen. You will do this in the Interactions pane.
Select a file with a picture in it by typing
String fileName = FileChooser.pickAFile();
in the
Interactions pane. When the file window appears, find the folder
mediaSources and in that folder select the file named butterfly2.jpg
Create a Picture
object that will contain the
image from the file: Picture pictureObj = new Picture(fileName);
What is the name of the reference variable that refers to that
Picture object?
To actually see the picture on the screen, type pictureObj.show();
Now type Java statements in the Interactions pane that will allow you to select the file beach.jpg from the mediaSources folder and display it on the screen. (You can use the same variable names as above, but be careful that you do not redeclare them.)
Leave the Interactions pane as it is (do not reset it), since
you will be using pictureObj
in the next exercise.
A picture (an object of the Picture
class) is composed
of many pixels (objects of the Pixel
class). Each pixel
has an (x,y) position in a picture, and has its color defined by red,
green and blue components. The Picture Explorer is a tool provided by
the textbook authors that lets us explore a picture by moving the
cursor around in the picture; we can see the x and y position values,
the color (red, green, blue components) of the pixel under the cursor.
To use this tool on the beach picture from Exercise 1, type
pictureObj.explore();
What are the red,green,blue values
for the pixel at (0,0)? for a pixel somewhere in the sand? for a pixel
somewhere in the water? What are the coordinates of the pixel at the
very bottom right-hand corner?
In this exercise, you will explore properties of Picture
objects and of Pixel
objects: a Pixel
object
is an object that stores a pixel; it comes from the book's predefined
classes. You are still using the Picture object from Exercises 1 and
2.
Type the following statement: System.out.println(pictureObj);
What properties of the Picture object are printed?
You can get the width and height of a Picture
object by using the methods getWidth
and getHeight
of the Picture
class.
These are object methods, that take no parameter, and
that return an int
.
Declare two variables width
and height
,
and assign them the width and height of pictureObj
.
Print the width and height in the format "Picture size is (width will
go here ) by (height will go here)". Then print the number of pixels
in the picture.
The Picture
class has an object
method getPixel
that can be used to get a pixel of a
picture at a specific location in the picture. Type the following
statement: Pixel pixelObj1 = pictureObj.getPixel(0,0);
It gets the pixel at the top left-hand corner, i.e. at coordinates
(0,0) of your picture, and stores a reference to it in the variable
pixelObj1
.
Type the following
statement: System.out.println(pixelObj1);
What properties
of the Pixel
object are printed?
Type the statements to print the properties of the pixels at
the top right-hand corner of the picture, at the bottom left-hand
corner, and of some pixel in the middle of the picture. Use different
variable names to refer to each of these Pixel
objects (for example
pixelObj2
, pixelObj3
,and pixelObj4
).
You can get the red, green and blue values of a Pixel
object by using the object
methods getRed
, getGreen
and getBlue
of the Pixel
class; they
return int
values.
Declare int
variables redValue
,
greenValue
and blueValue
, and assign them
the corresponding values from pixelObj1
. Print these
values in the format "Pixel red, green, blue values are: (red, green
blue values will go here, separated by commas)".
You will now create a complete program in a class called
ShowProperties
that will call the methods of
the Picture
class and the Pixel
class used
in the previous exercises. The main method
of ShowProperties
will get an image from a file, display
the picture on the screen, print its width and height, get the pixel
at coordinates (0,0) in that picture, and print the pixel's color
values: all necessary steps have been seen in the previous
exercises.
Enter the code for the program in the Definitions pane, save it as ShowProperties.java, and run it.
import java.awt.*; public class ShowProperties { public static void main (String[] args) { /* enter your code here */ } }