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. The original image can be read from a file in JPEG, GIF, or PNG format, or the user can create a blank image of a given size. This class includes methods for displaying the image in a window on the screen or saving to a file.

By default, pixel (x, y) is column x, row y, where (0, 0) is upper left. The method setOriginLowerLeft() change the origin to the lower left.

For additional documentation, see Section 3.1 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Picture(int w, int h)
    Create a blank w-by-h picture, where each pixel is black.
    Picture(File file)
    Create a picture by reading in a .png, .gif, or .jpg from a File.
    Picture(String filename)
    Create a picture by reading in a .png, .gif, or .jpg from the given filename or URL name.
    Copy constructor.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    void
    Opens a save dialog box when the user selects "Save As" from the menu.
    boolean
    Is this Picture equal to obj?
    get(int i, int j)
    Return the color of pixel (i, j).
    Return a JLabel containing this Picture, for embedding in a JPanel, JFrame or other GUI widget.
    int
    Return the height of the picture in pixels.
    static void
    main(String[] args)
    Test client.
    void
    save(File file)
    Save the picture to a file in a standard image format.
    void
    save(String name)
    Save the picture to a file in a standard image format.
    void
    set(int i, int j, Color c)
    Set the color of pixel (i, j) to c.
    void
    Set the origin to be the lower left pixel.
    void
    Set the origin to be the upper left pixel.
    void
    Display the picture in a window on the screen.
    int
    Return the width of the picture in pixels.

    Methods inherited from class java.lang.Object Link icon

    getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details Link icon

    • Picture Link icon

      public Picture(int w, int h)
      Create a blank w-by-h picture, where each pixel is black.
    • Picture Link icon

      public Picture(Picture pic)
      Copy constructor.
    • Picture Link icon

      public Picture(String filename)
      Create a picture by reading in a .png, .gif, or .jpg from the given filename or URL name.
    • Picture Link icon

      public Picture(File file)
      Create a picture by reading in a .png, .gif, or .jpg from a File.
  • Method Details Link icon

    • getJLabel Link icon

      public JLabel getJLabel()
      Return a JLabel containing this Picture, for embedding in a JPanel, JFrame or other GUI widget.
    • setOriginUpperLeft Link icon

      public void setOriginUpperLeft()
      Set the origin to be the upper left pixel.
    • setOriginLowerLeft Link icon

      public void setOriginLowerLeft()
      Set the origin to be the lower left pixel.
    • show Link icon

      public void show()
      Display the picture in a window on the screen.
    • height Link icon

      public int height()
      Return the height of the picture in pixels.
    • width Link icon

      public int width()
      Return the width of the picture in pixels.
    • get Link icon

      public Color get(int i, int j)
      Return the color of pixel (i, j).
    • set Link icon

      public void set(int i, int j, Color c)
      Set the color of pixel (i, j) to c.
    • equals Link icon

      public boolean equals(Object obj)
      Is this Picture equal to obj?
      Overrides:
      equals in class Object
    • save Link icon

      public void save(String name)
      Save the picture to a file in a standard image format. The filetype must be .png or .jpg.
    • save Link icon

      public void save(File file)
      Save the picture to a file in a standard image format.
    • actionPerformed Link icon

      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 Link icon

      public static void main(String[] args)
      Test client. Reads a picture specified by the command-line argument, and shows it in a window on the screen.