Gain experience with the manipulation of singly-dimension lists
Continue to practice class implementation
Demonstrate effectiveness of simple list manipulations for problem
solving
Files
Two files need to be downloaded:
Manipulator.java (a homework template) and PixelGrid.class. The
two files should be kept
in the same folder.
One file needs to be
submitted:
Manipulator.java. Class
Manipulator accesses an modifies a graphical image that
is stored as a PixelGrid, where a
PixelGrid is a Java class that provides a GUI supporting the display
and manipulation of an image (e.g., a JPG or GIF file). As a
convenience, we have included a method main()
in Manipulator.java that creates and runs the GUI.
Background
Class PixelGrid is a GUI that supports the loading, displaying and flipping of images. It is
only a partial tester of your
Manipulator code. When a new PixelGrid is default
constructed, a GUI is constructed with three buttons and an image display
area
If the load button is selected, then a file chooser dialog
opens
If an image file is indicated (e.g., a JPG or GIF file), it
is displayed in the GUI. The image that we manipulated is
available
The horizontal flip button, reverses the current image in a left
side to right side manner
The vertical flip button, reverses the current image in a top
side to bottom side manner
a
To directly support its activities PixelGrid provides a
constructor and methods for the accessing and manipulating of individual
pixels of a specified image:
PixelGrid(): constructs a
new PixelGrid to represent a GUI for the image manipulation
int getGridWidth(): returns the width of
the image (i.e., the number of pixels wide)
int getGridHeight(): returns the height
of the image (i.e., the number of pixels high)
int getPixel(int x, int y): returns
an integer representation of the pixel at location
(x, y) f the image
void setPixel(int x, int y, int v):
sets the pixel at location (x, y) of the
image to have value v
void setVisible(boolean b): sets the visibility of the GUI
according to the value of b
Class PixelGrid uses class Manipulator for doing
row and column pixel access and
manipulation
It is your responsibility to complete the design and implementation of
class Manipulator with its one constructor and seven methods. Each
of them has a straightforward implementation, so think and design before
you code
Manipulator.java requirements
Private attribute
PixelGrid grid: representation of a
graphical image
Constructor
Manipulator(PixelGrid g): sets the
PixelGrid of the new Manipulator object to be g
Public member methods
int[] getRow(int y): returns a new
array that represents the row of pixels in the object's
PixelGrid whose y-coordinate is y. This method will have
to go through each pixel in the specified row, and create a new array
containing each of these pixels
int[] getColumn(int x): returns a
new array that represents the column of pixels in the object's
PixelGrid whose x-coordinate is
x
void setRow(int y, int[] r): sets
the row of pixels with y-coordinate y in
the object's PixelGrid to
the values in
r (i.e., r[0], r[1], ... is copied into the PixelGrid
to replace the pixels whose y-coordinate is y)
void setColumn(int x, int[] c): sets
the column of pixels with x-coordinate x
in the PixelGrid to
the values in c (i.e., c[0], c[1], ... is
copied into the PixelGrid to replace the pixels whose
x-coordinate is
x)
void flipVertical(): flips the
object's PixelGrid upside down using the
following algorithm (i.e.,
for each x-coordinate x from the
interval 0 .. n-1, where
n is the width of the
PixelGrid
Create an array c
representing the column of pixels with x-coordinate
x in the
PixelGrid
Reverse column c
Set the column of pixels with x-coordinate
x in the
PixelGrid to
the values in
c
void flipHorizontal(): flips the
object's PixelGrid left to right using
the following algorithm
for each y-coordinate y from the interval 0 ..
n-1, where
n is the height of the PixelGrid
Create an array r
representing the row of pixels with y-coordinate
y in the
PixelGrid
Reverse row r
Set the row of pixels with y-coordinate y in the
PixelGrid to
the values in
r
Public class method (i.e., methods with modifier
static)
static void reverse(int[] list):
reverses the order of the element values in list