类 GrayscalePicture
- java.lang.Object
-
- edu.princeton.cs.algs4.GrayscalePicture
-
- 所有已实现的接口:
java.awt.event.ActionListener,java.util.EventListener
public final class GrayscalePicture extends java.lang.Object implements java.awt.event.ActionListenerThis class provides methods for manipulating individual pixels of a grayscale image. The original image can be read from aPNG,GIF, orJPEGfile 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()andset()methods useColorobjects to get or set the color of the specified pixel. TheColorobjects are converted to grayscale if they have different values for the R, G, and B channels. ThegetGrayscale()andsetGrayscale()methods use an 8-bitintto encode the grayscale value, thereby avoiding the need to create temporaryColorobjects.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(even though, in principle, only ~ W H bytes are needed).For additional documentation, see Section 3.1 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne. See
Picturefor a version that supports 32-bit RGB color images.
-
-
构造器概要
构造器 构造器 说明 GrayscalePicture(int width, int height)Creates awidth-by-heightpicture, withwidthcolumns andheightrows, where each pixel is black.GrayscalePicture(GrayscalePicture picture)Creates a new grayscale picture that is a deep copy of the argument picture.GrayscalePicture(java.lang.String filename)Creates a grayscale picture by reading an image from a file or URL.
-
方法概要
修饰符和类型 方法 说明 voidactionPerformed(java.awt.event.ActionEvent e)Opens a save dialog box when the user selects "Save As" from the menu.booleanequals(java.lang.Object other)Returns true if this picture is equal to the argument picture.java.awt.Colorget(int col, int row)Returns the grayscale value of pixel (col,row) as aColor.intgetGrayscale(int col, int row)Returns the grayscale value of pixel (col,row) as anintbetween 0 and 255.javax.swing.JLabelgetJLabel()Returns aJLabelcontaining this picture, for embedding in aJPanel,JFrameor other GUI widget.inthashCode()This operation is not supported because pictures are mutable.intheight()Returns the height of the picture.static voidmain(java.lang.String[] args)Unit tests thisPicturedata type.voidsave(java.io.File file)Saves the picture to a file in a PNG or JPEG image format.voidsave(java.lang.String name)Saves the picture to a file in either PNG or JPEG format.voidset(int col, int row, java.awt.Color color)Sets the color of pixel (col,row) to the given grayscale value.voidsetGrayscale(int col, int row, int gray)Sets the color of pixel (col,row) to the given grayscale value between 0 and 255.voidsetOriginLowerLeft()Sets the origin to be the lower left pixel.voidsetOriginUpperLeft()Sets the origin to be the upper left pixel.voidshow()Displays the picture in a window on the screen.java.lang.StringtoString()Returns a string representation of this picture.intwidth()Returns the width of the picture.
-
-
-
构造器详细资料
-
GrayscalePicture
public GrayscalePicture(int width, int height)Creates awidth-by-heightpicture, withwidthcolumns andheightrows, where each pixel is black.- 参数:
width- the width of the pictureheight- the height of the picture- 抛出:
java.lang.IllegalArgumentException- ifwidthis negativejava.lang.IllegalArgumentException- ifheightis negative
-
GrayscalePicture
public GrayscalePicture(GrayscalePicture picture)
Creates a new grayscale picture that is a deep copy of the argument picture.- 参数:
picture- the picture to copy- 抛出:
java.lang.IllegalArgumentException- ifpictureisnull
-
GrayscalePicture
public GrayscalePicture(java.lang.String filename)
Creates a grayscale picture by reading an image from a file or URL.- 参数:
filename- the name of the file (.png, .gif, or .jpg) or URL.- 抛出:
java.lang.IllegalArgumentException- if cannot read imagejava.lang.IllegalArgumentException- iffilenameisnull
-
-
方法详细资料
-
getJLabel
public javax.swing.JLabel getJLabel()
Returns aJLabelcontaining this picture, for embedding in aJPanel,JFrameor other GUI widget.- 返回:
- 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.- 返回:
- the height of the picture (in pixels)
-
width
public int width()
Returns the width of the picture.- 返回:
- the width of the picture (in pixels)
-
get
public java.awt.Color get(int col, int row)Returns the grayscale value of pixel (col,row) as aColor.- 参数:
col- the column indexrow- the row index- 返回:
- the grayscale value of pixel (
col,row) - 抛出:
java.lang.IllegalArgumentException- unless both0 <= col < widthand0 <= row < height
-
getGrayscale
public int getGrayscale(int col, int row)Returns the grayscale value of pixel (col,row) as anintbetween 0 and 255. Using this method can be more efficient thanget(int, int)because it does not create aColorobject.- 参数:
col- the column indexrow- the row index- 返回:
- the 8-bit integer representation of the grayscale value of pixel (
col,row) - 抛出:
java.lang.IllegalArgumentException- unless both0 <= col < widthand0 <= row < height
-
set
public void set(int col, int row, java.awt.Color color)Sets the color of pixel (col,row) to the given grayscale value.- 参数:
col- the column indexrow- the row indexcolor- the color (converts to grayscale if color is not a shade of gray)- 抛出:
java.lang.IllegalArgumentException- unless both0 <= col < widthand0 <= row < heightjava.lang.IllegalArgumentException- ifcolorisnull
-
setGrayscale
public void setGrayscale(int col, int row, int gray)Sets the color of pixel (col,row) to the given grayscale value between 0 and 255.- 参数:
col- the column indexrow- the row indexgray- the 8-bit integer representation of the grayscale value- 抛出:
java.lang.IllegalArgumentException- unless both0 <= col < widthand0 <= row < height
-
equals
public boolean equals(java.lang.Object other)
Returns true if this picture is equal to the argument picture.- 覆盖:
equals在类中java.lang.Object- 参数:
other- the other picture- 返回:
trueif this picture is the same dimension asotherand if all pixels have the same color;falseotherwise
-
toString
public java.lang.String toString()
Returns a string representation of this picture. The result is awidth-by-heightmatrix of pixels, where the grayscale value of a pixel is an integer between 0 and 255.- 覆盖:
toString在类中java.lang.Object- 返回:
- a string representation of this picture
-
hashCode
public int hashCode()
This operation is not supported because pictures are mutable.- 覆盖:
hashCode在类中java.lang.Object- 返回:
- does not return a value
- 抛出:
java.lang.UnsupportedOperationException- if called
-
save
public void save(java.lang.String name)
Saves the picture to a file in either PNG or JPEG format. The filetype extension must be either .png or .jpg.- 参数:
name- the name of the file- 抛出:
java.lang.IllegalArgumentException- ifnameisnull
-
save
public void save(java.io.File file)
Saves the picture to a file in a PNG or JPEG image format.- 参数:
file- the file- 抛出:
java.lang.IllegalArgumentException- iffileisnull
-
actionPerformed
public void actionPerformed(java.awt.event.ActionEvent e)
Opens a save dialog box when the user selects "Save As" from the menu.- 指定者:
actionPerformed在接口中java.awt.event.ActionListener
-
main
public static void main(java.lang.String[] args)
Unit tests thisPicturedata type. Reads a picture specified by the command-line argument, and shows it in a window on the screen.- 参数:
args- the command-line arguments
-
-