Package stdlib

Class Picture

java.lang.Object
stdlib.Picture
All Implemented Interfaces:
ActionListener, EventListener

public final class Picture extends Object implements 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 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

    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.
    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

    Modifier and Type
    Method
    Description
    void
    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}.
    Returns a JLabel containing this picture, for embedding in a JPanel, JFrame or other GUI widget.
    int
    Returns the height of the picture.
    static void
    main(String[] args)
    Unit tests this Picture data type.
    void
    save(String name)
    Saves the picture to a file in either PNG or JPEG format.
    void
    set(int col, int row, RGBColor color)
    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
    Displays the picture in a window on the screen.
    int
    Returns the width of the picture.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • 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:
      IllegalArgumentException - if width is negative or zero
      IllegalArgumentException - if height is negative or zero
    • 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:
      IllegalArgumentException - if picture is null
    • Picture

      public Picture(String name)
      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 image
      IllegalArgumentException - if name is null
  • Method Details

    • getJLabel

      public 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.
    • 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 RGBColor 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:
      IllegalArgumentException - unless both 0 <= col < width and 0 <= row < height
    • set

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

      public void save(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:
      IllegalArgumentException - if name is null
    • actionPerformed

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

      public static void main(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