Package stdlib
Class Picture
java.lang.Object
stdlib.Picture
- All Implemented Interfaces:
ActionListener
,EventListener
This class provides methods for manipulating individual pixels of
an image using the RGB color format. The alpha component (for transparency)
is not currently supported.
The original image can be read from a
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 RGBColor
objects to get
or set the color of the specified pixel.
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.
- Author:
- Robert Sedgewick, Kevin Wayne
-
Constructor Summary
ConstructorDescriptionPicture
(int width, int height) Creates awidth
-by-height
picture, withwidth
columns andheight
rows, where each pixel is black.Creates a picture by reading an image from a file or URL.Creates a new picture that is a deep copy of the argument picture. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Opens a save dialog box when the user selects "Save As" from the menu.get
(int col, int row) Returns the color of pixel (col
,row
) as a {Color}.int
height()
Returns the height of the picture.static void
Unit tests thisPicture
data type.void
Saves the picture to a file in either PNG or JPEG format.void
Sets the color of pixel (col
,row
) to given color.void
Sets the origin to be the lower left pixel.void
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.
-
Constructor Details
-
Picture
public Picture(int width, int height) Creates awidth
-by-height
picture, withwidth
columns andheight
rows, where each pixel is black.- Parameters:
width
- the width of the pictureheight
- the height of the picture- Throws:
IllegalArgumentException
- ifwidth
is negative or zeroIllegalArgumentException
- ifheight
is negative or zero
-
Picture
Creates a new picture that is a deep copy of the argument picture.- Parameters:
picture
- the picture to copy- Throws:
IllegalArgumentException
- ifpicture
isnull
-
Picture
Creates a picture by reading an image from a file or URL.- Parameters:
name
- the name of the file (.png, .gif, or .jpg) or URL.- Throws:
IllegalArgumentException
- if cannot read imageIllegalArgumentException
- ifname
isnull
-
-
Method Details
-
getJLabel
- Returns:
- the
JLabel
-
setOriginUpperLeft
public void setOriginUpperLeft()Sets the origin to be the upper left pixel. This is the default. -
setOriginLowerLeft
public void setOriginLowerLeft()Sets the origin to be the lower left pixel. -
show
public void show()Displays the picture in a window on the screen. -
height
public int height()Returns the height of the picture.- Returns:
- the height of the picture (in pixels)
-
width
public int width()Returns the width of the picture.- Returns:
- the width of the picture (in pixels)
-
get
Returns the color of pixel (col
,row
) as a {Color}.- Parameters:
col
- the column indexrow
- the row index- Returns:
- the color of pixel (
col
,row
) - Throws:
IllegalArgumentException
- unless both0 <= col < width
and0 <= row < height
-
set
Sets the color of pixel (col
,row
) to given color.- Parameters:
col
- the column indexrow
- the row indexcolor
- the color- Throws:
IllegalArgumentException
- unless both0 <= col < width
and0 <= row < height
IllegalArgumentException
- ifcolor
isnull
-
save
Saves the picture to a file in either PNG or JPEG format. The filetype extension must be either .png or .jpg.- Parameters:
name
- the name of the file- Throws:
IllegalArgumentException
- ifname
isnull
-
actionPerformed
Opens a save dialog box when the user selects "Save As" from the menu.- Specified by:
actionPerformed
in interfaceActionListener
-
main
Unit tests thisPicture
data type. Reads a picture specified by the command-line argument, and shows it in a window on the screen.- Parameters:
args
- the command-line arguments
-