img
Class DigitalPicture

java.lang.Object
  extended by img.DigitalPicture
All Implemented Interfaces:
AbstractDigitalPicture

public class DigitalPicture
extends java.lang.Object
implements AbstractDigitalPicture

This class encapsulates a digital picture, which can be created from a file name, from another image, or from a width and height specification. A digital picture has accessor and mutator methods for its pixels. A digital picture has an associated string which serves as the picture's title. A digital picture can be displayed graphically. As well, some basic interaction with a digital picture is provided via the PictureExplorer. This class is based on the Picture class from Guzdial and Ericson, with explanatory comments and modifications provided by various contributors: sdc@cs.albany.edu, ericson@cc.gatech.edu. mb@cse.yorku.ca


Constructor Summary
DigitalPicture()
          Default constructor, which instantiates a blank picture consisting of only white pixels, with a default width of 200 and a default height of 100.
DigitalPicture(DigitalPicture copyPicture)
          Constructs a new instance of this class that has the same state as the passed instance.
DigitalPicture(int width, int height)
          Constructs a picture with the width and height as specified by the passed parameter values.
DigitalPicture(java.lang.String fileName)
          Constructs a picture from the image in the filename that is passed as parameter.
 
Method Summary
 void addMessage(java.lang.String message, int xPos, int yPos)
          Draw the passed string on this picture at the specified coordinates.
 void blacken(int row)
          The method causes all of the pixels in the specified row to be changed to a black colour.
 void copyPicture(DigitalPicture sourcePicture)
          Causes all attributes of this object to be copied from the passed source picture.
 java.awt.Graphics2D createGraphics()
          Returns the Graphics2D instance (encapsulates the graphics context for the platform upon which the virtual machine is running), which can then be used to do 2D drawing on the picture
 void explore()
          Causes this picture to be shown in a new PictureExplorer window.
 int getBasicPixel(int x, int y)
          Returns the colour information about the specified pixel, as encoded in an int value.
 java.awt.image.BufferedImage getBufferedImage()
          Returns the buffered image associated with this picture.
 java.lang.String getExtension()
          Returns the extension of the filename used to create this picture (e.g., jpg or bmp), if one exists (otherwise, null is returned).
 java.lang.String getFileName()
          Get the name of the file associated with this digital picture, if one exists.
 java.awt.Graphics getGraphics()
          Returns the Graphics instance (encapsulates the graphics context for the platform upon which the virtual machine is running).
 java.awt.Graphics2D getGraphics2D()
          Returns the Graphics2D instance (encapsulates the graphics context for the platform upon which the virtual machine is running).
 int getHeight()
          Returns the height of this picture in pixels.
 java.awt.Image getImage()
          Returns the Image from the picture
 PictureFrame getPictureFrame()
          Returns the PictureFrame object that associated with this picture (it may be null)
 DigitalPicture getPictureWithHeight(int height)
          Method to create a new picture of the passed height.
 Pixel getPixel(int x, int y)
          Returns the Pixel object at the specified (x,y) coordinate.
 Pixel[] getPixels()
          Returns a one-dimensional array of Pixels for this picture
 java.lang.String getTitle()
          Get the string that encapsulates the title that is associated with this digital picture.
 java.awt.geom.Rectangle2D getTranslationEnclosingRectangle(java.awt.geom.AffineTransform trans)
          Method to get the coordinates of the enclosing rectangle after this transformation is applied to the current picture
 int getWidth()
          Returns the width of this picture in pixels.
 void load(java.awt.Image image)
          Load the buffered image with the passed image
 void load(java.lang.String fileName)
          Loads the picture from the passed file name, if a picture exists in the file.
 void modifyRowColour(int row, java.awt.Color theNewColor)
          Cause the pixels in the specified row to change to the specifed colour.
 void repaint()
          Forces this picture to redraw itself.
 void setAllPixelsToAColor(java.awt.Color color)
          Set the color of all of the pixels to the passed color
 void setBasicPixel(int x, int y, int rgb)
          Sets the colour of the specified pixel, as encoded in the passed int value.
 void setPictureFrame(PictureFrame pictureFrame)
          Set the passed PictureFrame object to be associated with this picture.
 void setTitle(java.lang.String title)
          Set the string that encapsulates the title that is associated with this digital picture.
 void show()
          Causes this picture to be displayed graphically (using the PictureFrame object that is associated with this picture).
 java.lang.String toString()
          Return a string representation of this Picture.
 void write(java.lang.String fileName)
          Method to write the contents of the picture to a file with the passed name
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DigitalPicture

public DigitalPicture()
Default constructor, which instantiates a blank picture consisting of only white pixels, with a default width of 200 and a default height of 100. The default title is "New Picture".


DigitalPicture

public DigitalPicture(int width,
                      int height)
Constructs a picture with the width and height as specified by the passed parameter values. All of the pixels are set to the default colour of white. The default title is "New Picture".

Parameters:
width - the width of the desired picture
height - the height of the desired picture

DigitalPicture

public DigitalPicture(java.lang.String fileName)
Constructs a picture from the image in the filename that is passed as parameter. The width and height of the picture will be determined by the images that is contained in the file. The title will be the name of the file. If the specified file does not contain an image, then this constructor causes a runtime exception to occur.

Parameters:
fileName - the name of the file that contains the source image
Throws:
java.lang.Exception - as described above

DigitalPicture

public DigitalPicture(DigitalPicture copyPicture)
Constructs a new instance of this class that has the same state as the passed instance.

Parameters:
copyPicture - the picture to copy from
Method Detail

getExtension

public java.lang.String getExtension()
Returns the extension of the filename used to create this picture (e.g., jpg or bmp), if one exists (otherwise, null is returned).

Returns:
as described above

copyPicture

public void copyPicture(DigitalPicture sourcePicture)
Causes all attributes of this object to be copied from the passed source picture.

Parameters:
sourcePicture - the picture object from which the attributes should be copied.

setAllPixelsToAColor

public void setAllPixelsToAColor(java.awt.Color color)
Set the color of all of the pixels to the passed color

Parameters:
color - the new color for all of the pixels

blacken

public void blacken(int row)
The method causes all of the pixels in the specified row to be changed to a black colour.

Parameters:
row -

modifyRowColour

public void modifyRowColour(int row,
                            java.awt.Color theNewColor)
Cause the pixels in the specified row to change to the specifed colour. The row indexing is the same as the pixel indexing (e.g., the topmost row has index position 0)

Parameters:
row - the index of the row to be changed
theNewColor - the new colour of the row

getBufferedImage

public java.awt.image.BufferedImage getBufferedImage()
Returns the buffered image associated with this picture.

Specified by:
getBufferedImage in interface AbstractDigitalPicture
Returns:
as described above

getGraphics

public java.awt.Graphics getGraphics()
Returns the Graphics instance (encapsulates the graphics context for the platform upon which the virtual machine is running).

Returns:
as described above

getGraphics2D

public java.awt.Graphics2D getGraphics2D()
Returns the Graphics2D instance (encapsulates the graphics context for the platform upon which the virtual machine is running).

Returns:
as described above

createGraphics

public java.awt.Graphics2D createGraphics()
Returns the Graphics2D instance (encapsulates the graphics context for the platform upon which the virtual machine is running), which can then be used to do 2D drawing on the picture

Returns:
as described above

getFileName

public java.lang.String getFileName()
Get the name of the file associated with this digital picture, if one exists. The digital picture may not have been created from a file, in which case this method returns null.

Specified by:
getFileName in interface AbstractDigitalPicture
Returns:
as described above

getTitle

public java.lang.String getTitle()
Get the string that encapsulates the title that is associated with this digital picture.

Specified by:
getTitle in interface AbstractDigitalPicture
Returns:
as described above

setTitle

public void setTitle(java.lang.String title)
Set the string that encapsulates the title that is associated with this digital picture.

Specified by:
setTitle in interface AbstractDigitalPicture

getWidth

public int getWidth()
Returns the width of this picture in pixels.

Specified by:
getWidth in interface AbstractDigitalPicture
Returns:
as described above.

getHeight

public int getHeight()
Returns the height of this picture in pixels.

Specified by:
getHeight in interface AbstractDigitalPicture
Returns:
as described above.

getPictureFrame

public PictureFrame getPictureFrame()
Returns the PictureFrame object that associated with this picture (it may be null)

Returns:
as described above.

setPictureFrame

public void setPictureFrame(PictureFrame pictureFrame)
Set the passed PictureFrame object to be associated with this picture.

Parameters:
pictureFrame - the picture frame to use

getImage

public java.awt.Image getImage()
Returns the Image from the picture

Specified by:
getImage in interface AbstractDigitalPicture
Returns:
as described above.

getBasicPixel

public int getBasicPixel(int x,
                         int y)
Returns the colour information about the specified pixel, as encoded in an int value.

Specified by:
getBasicPixel in interface AbstractDigitalPicture
Parameters:
x - the x position of the pixel
y - the y position of the pixel
Returns:
as described above

setBasicPixel

public void setBasicPixel(int x,
                          int y,
                          int rgb)
Sets the colour of the specified pixel, as encoded in the passed int value.

Specified by:
setBasicPixel in interface AbstractDigitalPicture
Parameters:
x - the x position of the pixel
y - the y position of the pixel
rgb - the new rgb value of the pixel (alpha, red, green, blue)

getPixel

public Pixel getPixel(int x,
                      int y)
Returns the Pixel object at the specified (x,y) coordinate.

Specified by:
getPixel in interface AbstractDigitalPicture
Parameters:
x - the x position of the pixel
y - the y position of the pixel
Returns:
as descibed above

getPixels

public Pixel[] getPixels()
Returns a one-dimensional array of Pixels for this picture

Returns:
a one-dimensional array of Pixel objects starting with y=0 to y=height-1 and x=0 to x=width-1.

load

public void load(java.awt.Image image)
Load the buffered image with the passed image

Specified by:
load in interface AbstractDigitalPicture
Parameters:
image - the image to use

show

public void show()
Causes this picture to be displayed graphically (using the PictureFrame object that is associated with this picture).

Specified by:
show in interface AbstractDigitalPicture

explore

public void explore()
Causes this picture to be shown in a new PictureExplorer window.


repaint

public void repaint()
Forces this picture to redraw itself. Necessary after the pixels in a picture have been changed.


load

public void load(java.lang.String fileName)
          throws java.lang.Exception
Loads the picture from the passed file name, if a picture exists in the file. If not, this method throws an exception.

Specified by:
load in interface AbstractDigitalPicture
Parameters:
fileName - the file name to use to load the picture from
Throws:
java.lang.Exception - as described above

addMessage

public void addMessage(java.lang.String message,
                       int xPos,
                       int yPos)
Draw the passed string on this picture at the specified coordinates. The default font is Helvetica, bold, with point size 16.

Parameters:
message - the message to draw on the buffered image
xPos - the leftmost point of the string in x
yPos - the bottom of the string in y

getPictureWithHeight

public DigitalPicture getPictureWithHeight(int height)
Method to create a new picture of the passed height. The aspect ratio of the width and height will stay the same.

Parameters:
height - the desired height
Returns:
the resulting picture

write

public void write(java.lang.String fileName)
Method to write the contents of the picture to a file with the passed name

Parameters:
fileName - the name of the file to write the picture to

getTranslationEnclosingRectangle

public java.awt.geom.Rectangle2D getTranslationEnclosingRectangle(java.awt.geom.AffineTransform trans)
Method to get the coordinates of the enclosing rectangle after this transformation is applied to the current picture

Returns:
the enclosing rectangle

toString

public java.lang.String toString()
Return a string representation of this Picture. The string contains the height and width of the image.

Overrides:
toString in class java.lang.Object
Returns:
as described above.