public final class Picture extends Object implements ActionListener
Pixel (x, y) is column x and row y. By default, the origin (0, 0) is upper left, which is a common convention in image processing. The method setOriginLowerLeft() change the origin to the lower left.
Constructor and Description |
---|
Picture(File file)
Initializes a picture by reading in a .png, .gif, or .jpg from a file.
|
Picture(int width,
int height)
Initializes a blank width-by-height picture, with width columns
and height rows, where each pixel is black.
|
Picture(Picture picture)
Initializes a new picture that is a deep copy of the argument picture.
|
Picture(String filename)
Initializes a picture by reading 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)
Returns the color of pixel (col, row).
|
JLabel |
getJLabel()
Returns a JLabel containing this picture, for embedding in a JPanel,
JFrame or other GUI widget.
|
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 a standard image 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 |
show()
Displays the picture in a window on the screen.
|
int |
width()
Returns the width of the picture.
|
public Picture(int width, int height)
width
- the width of the pictureheight
- the height of the picturepublic Picture(Picture picture)
picture
- the picture to copypublic Picture(String filename)
filename
- the name of the file (.png, .gif, or .jpg) or URL.public Picture(File file)
file
- the filepublic JLabel getJLabel()
public 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 indexIndexOutOfBoundsException
- unless both 0 ≤ col < width
and 0 ≤ row < heightpublic void set(int col, int row, Color color)
col
- the column indexrow
- the row indexcolor
- the colorIndexOutOfBoundsException
- unless both 0 ≤ col < width
and 0 ≤ row < heightNullPointerException
- if color is nullpublic boolean equals(Object other)
public int hashCode()
hashCode
in class Object
UnsupportedOperationException
- if calledpublic void save(String name)
name
- the name of the filepublic void save(File file)
file
- the filepublic void actionPerformed(ActionEvent e)
actionPerformed
in interface ActionListener
public static void main(String[] args)