Class Picture

  • All Implemented Interfaces:
    java.awt.event.ActionListener, java.util.EventListener

    public final class Picture
    extends java.lang.Object
    implements java.awt.event.ActionListener
    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 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:

      int r = (rgb >> 16) & 0xFF;
      int g = (rgb >>  8) & 0xFF;
      int b = (rgb >>  0) & 0xFF;
      
    Given the RGB components (8-bits each) of a color, the following statement packs it into a 32-bit 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.

    • Constructor Summary

      Constructors 
      Constructor Description
      Picture​(int width, int height)
      Creates a width-by-height picture, with width columns and height rows, where each pixel is black.
      Picture​(java.io.File file)
      Creates a picture by reading the image from a PNG, GIF, or JPEG file.
      Picture​(java.lang.String filename)
      Creates a picture by reading an image from a file or URL.
      Picture​(Picture picture)
      Creates a new picture that is a deep copy of the argument picture.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void actionPerformed​(java.awt.event.ActionEvent e)
      Opens a save dialog box when the user selects "Save As" from the menu.
      int[] compare​(Picture pic2, int margin)  
      boolean equals​(java.lang.Object oth)  
      java.awt.Color get​(int col, int row)
      Returns the color of pixel (col, row) as a Color.
      javax.swing.JLabel getJLabel()
      Returns a JLabel containing this picture, for embedding in a JPanel, JFrame or other GUI widget.
      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​(java.lang.String[] args)
      Unit tests this Picture data type.
      void save​(java.io.File file)
      Saves the picture to a file in a PNG or JPEG image format.
      void save​(java.lang.String name)
      Saves the picture to a file in either PNG or JPEG format.
      Picture scale​(int factor)
      Scale the picture up by a factor, resulting in larger pixelated image.
      void set​(int col, int row, java.awt.Color color)
      Sets the color of pixel (col, row) to given color.
      void setFilename​(java.lang.String fname)
      Sets the filename.
      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.
      java.lang.String toString()
      Returns a string representation of this picture.
      int width()
      Returns the width of the picture.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Picture

        public Picture​(int width,
                       int height)
        Creates a width-by-height picture, with width columns and height rows, where each pixel is black.
        Parameters:
        width - the width of the picture
        height - the height of the picture
        Throws:
        java.lang.IllegalArgumentException - if width is negative
        java.lang.IllegalArgumentException - if height is negative
      • Picture

        public Picture​(Picture picture)
        Creates a new picture that is a deep copy of the argument picture.
        Parameters:
        picture - the picture to copy
        Throws:
        java.lang.IllegalArgumentException - if picture is null
      • Picture

        public Picture​(java.lang.String filename)
        Creates a picture by reading an image from a file or URL.
        Parameters:
        filename - the name of the file (.png, .gif, or .jpg) or URL.
        Throws:
        java.lang.IllegalArgumentException - if cannot read image
        java.lang.IllegalArgumentException - if filename is null
      • Picture

        public Picture​(java.io.File file)
        Creates a picture by reading the image from a PNG, GIF, or JPEG file.
        Parameters:
        file - the file
        Throws:
        java.lang.IllegalArgumentException - if cannot read image
        java.lang.IllegalArgumentException - if file is null
    • Method Detail

      • getJLabel

        public javax.swing.JLabel getJLabel()
        Returns a JLabel containing this picture, for embedding in a JPanel, JFrame or other GUI widget.
        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.
      • setFilename

        public void setFilename​(java.lang.String fname)
        Sets the filename.
        Parameters:
        fname - the filename
      • 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

        public java.awt.Color get​(int col,
                                  int row)
        Returns the color of pixel (col, row) as a Color.
        Parameters:
        col - the column index
        row - the row index
        Returns:
        the color of pixel (col, row)
        Throws:
        java.lang.IllegalArgumentException - unless both 0 <= col < width and 0 <= row < height
      • getRGB

        public int getRGB​(int col,
                          int row)
        Returns the color of pixel (col, row) as an int. Using this method can be more efficient than get(int, int) because it does not create a Color object.
        Parameters:
        col - the column index
        row - the row index
        Returns:
        the integer representation of the color of pixel (col, row)
        Throws:
        java.lang.IllegalArgumentException - unless both 0 <= col < width and 0 <= row < height
      • set

        public void set​(int col,
                        int row,
                        java.awt.Color color)
        Sets the color of pixel (col, row) to given color.
        Parameters:
        col - the column index
        row - the row index
        color - the color
        Throws:
        java.lang.IllegalArgumentException - unless both 0 <= col < width and 0 <= row < height
        java.lang.IllegalArgumentException - if color is null
      • setRGB

        public void setRGB​(int col,
                           int row,
                           int rgb)
        Sets the color of pixel (col, row) to given color.
        Parameters:
        col - the column index
        row - the row index
        rgb - the integer representation of the color
        Throws:
        java.lang.IllegalArgumentException - unless both 0 <= col < width and 0 <= row < height
      • scale

        public Picture scale​(int factor)
        Scale the picture up by a factor, resulting in larger pixelated image.
        Parameters:
        factor - scale multiplier
        Returns:
        the large Picture
        Throws:
        java.lang.IllegalArgumentException - unless factor >= 1
      • toString

        public java.lang.String toString()
        Returns a string representation of this picture. The result is a 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.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this picture
      • hashCode

        public int hashCode()
        This operation is not supported because pictures are mutable.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        does not return a value
        Throws:
        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.
        Parameters:
        name - the name of the file
        Throws:
        java.lang.IllegalArgumentException - if name is null
      • save

        public void save​(java.io.File file)
        Saves the picture to a file in a PNG or JPEG image format.
        Parameters:
        file - the file
        Throws:
        java.lang.IllegalArgumentException - if file is null
      • actionPerformed

        public void actionPerformed​(java.awt.event.ActionEvent e)
        Opens a save dialog box when the user selects "Save As" from the menu.
        Specified by:
        actionPerformed in interface java.awt.event.ActionListener
      • equals

        public boolean equals​(java.lang.Object oth)
        Overrides:
        equals in class java.lang.Object
      • compare

        public int[] compare​(Picture pic2,
                             int margin)
      • main

        public static void main​(java.lang.String[] args)
        Unit tests this Picture 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