public final class Picture extends Object implements ActionListener
PNG, GIF,
or JPEG file or the user can create a blank image of a given dimension.
This class includes methods for displaying the image in a window on
the screen or saving it to a file.
Pixel (col, row) is column col and row row.
By default, the origin (0, 0) is the pixel in the top-left corner,
which is a common convention in image processing.
The method setOriginLowerLeft() change the origin to the lower left.
The get() and set() methods use Color objects to get
or set the color of the specified pixel.
The getRGB() and setRGB() methods use a 32-bit int
to encode the color, thereby avoiding the need to create temporary
Color objects. The red (R), green (G), and blue (B) components
are encoded using the least significant 24 bits.
Given a 32-bit int encoding the color, the following code extracts
the RGB components:
Given the RGB components (8-bits each) of a color, the following statement packs it into a 32-bitint r = (rgb >> 16) & 0xFF; int g = (rgb >> 8) & 0xFF; int b = (rgb >> 0) & 0xFF;
int:
int rgb = (r << 16) + (g << 8) + (b << 0);
A W-by-H picture uses ~ 4 W H bytes of memory,
since the color of each pixel is encoded as a 32-bit int.
For additional documentation, see
Section 3.1 of
Computer Science: An Interdisciplinary Approach
by Robert Sedgewick and Kevin Wayne.
See GrayscalePicture for a version that supports grayscale images.
| Constructor and Description |
|---|
Picture(File file)
Creates a picture by reading the image from a PNG, GIF, or JPEG file.
|
Picture(int width,
int height)
Creates a
width-by-height picture, with width columns
and height rows, where each pixel is black. |
Picture(Picture picture)
Creates a new picture that is a deep copy of the argument picture.
|
Picture(String name)
Creates a picture by reading an image from a file or URL.
|
| Modifier and Type | Method and Description |
|---|---|
void |
actionPerformed(ActionEvent e)
Opens a save dialog box when the user selects "Save As" from the menu.
|
boolean |
equals(Object other)
Returns true if this picture is equal to the argument picture.
|
Color |
get(int col,
int row)
|
JLabel |
getJLabel()
|
int |
getRGB(int col,
int row)
Returns the color of pixel (
col, row) as an int. |
int |
hashCode()
This operation is not supported because pictures are mutable.
|
int |
height()
Returns the height of the picture.
|
static void |
main(String[] args)
Unit tests this
Picture data type. |
void |
save(File file)
Saves the picture to a file in a PNG or JPEG image format.
|
void |
save(String name)
Saves the picture to a file in either PNG or JPEG format.
|
void |
set(int col,
int row,
Color color)
Sets the color of pixel (
col, row) to given color. |
void |
setOriginLowerLeft()
Sets the origin to be the lower left pixel.
|
void |
setOriginUpperLeft()
Sets the origin to be the upper left pixel.
|
void |
setRGB(int col,
int row,
int rgb)
Sets the color of pixel (
col, row) to given color. |
void |
show()
Displays the picture in a window on the screen.
|
String |
toString()
Returns a string representation of this picture.
|
int |
width()
Returns the width of the picture.
|
public Picture(int width,
int height)
width-by-height picture, with width columns
and height rows, where each pixel is black.width - the width of the pictureheight - the height of the pictureIllegalArgumentException - if width is negative or zeroIllegalArgumentException - if height is negative or zeropublic Picture(Picture picture)
picture - the picture to copyIllegalArgumentException - if picture is nullpublic Picture(String name)
name - the name of the file (.png, .gif, or .jpg) or URL.IllegalArgumentException - if cannot read imageIllegalArgumentException - if name is nullpublic Picture(File file)
file - the fileIllegalArgumentException - if cannot read imageIllegalArgumentException - if file is nullpublic JLabel getJLabel()
JLabelpublic void setOriginUpperLeft()
public void setOriginLowerLeft()
public void show()
public int height()
public int width()
public Color get(int col, int row)
col - the column indexrow - the row indexcol, row)IllegalArgumentException - unless both 0 <= col < width and 0 <= row < heightpublic int getRGB(int col,
int row)
col, row) as an int.
Using this method can be more efficient than get(int, int) because
it does not create a Color object.col - the column indexrow - the row indexcol, row)IllegalArgumentException - unless both 0 <= col < width and 0 <= row < heightpublic void set(int col,
int row,
Color color)
col, row) to given color.col - the column indexrow - the row indexcolor - the colorIllegalArgumentException - unless both 0 <= col < width and 0 <= row < heightIllegalArgumentException - if color is nullpublic void setRGB(int col,
int row,
int rgb)
col, row) to given color.col - the column indexrow - the row indexrgb - the integer representation of the colorIllegalArgumentException - unless both 0 <= col < width and 0 <= row < heightpublic boolean equals(Object other)
public String toString()
width-by-height matrix of pixels,
where the color of a pixel is represented using 6 hex digits to encode
the red, green, and blue components.public int hashCode()
hashCode in class ObjectUnsupportedOperationException - if calledpublic void save(String name)
name - the name of the fileIllegalArgumentException - if name is nullpublic void save(File file)
file - the fileIllegalArgumentException - if file is nullpublic void actionPerformed(ActionEvent e)
actionPerformed in interface ActionListenerpublic static void main(String[] args)
Picture data type.
Reads a picture specified by the command-line argument,
and shows it in a window on the screen.args - the command-line arguments