Class SimplePicture

java.lang.Object
  extended by SimplePicture
All Implemented Interfaces:
DigitalPicture
Direct Known Subclasses:
Picture

public class SimplePicture
extends Object
implements DigitalPicture

A class that represents a simple picture. A simple picture may have an associated file name and a title. A simple picture has pixels, width, and height. A simple picture uses a BufferedImage to hold the pixels. You can show a simple picture in a PictureFrame (a JFrame). You can also explore a simple picture.

Author:
Barb Ericson ericson@cc.gatech.edu

Constructor Summary
SimplePicture()
          A Constructor that takes no arguments.
SimplePicture(BufferedImage image)
          A constructor that takes a buffered image
SimplePicture(int width, int height)
          A constructor that takes the width and height desired for a picture and creates a buffered image of that size.
SimplePicture(int width, int height, Color theColor)
          A constructor that takes the width and height desired for a picture and creates a buffered image of that size.
SimplePicture(SimplePicture copyPicture)
          A Constructor that takes a picture to copy information from
SimplePicture(String fileName)
          A Constructor that takes a file name and uses the file to create a picture
 
Method Summary
 void addMessage(String message, int xPos, int yPos)
          Method to draw a message as a string on the buffered image
 void copyPicture(SimplePicture sourcePicture)
          Method that will copy all of the passed source picture into the current picture object
 Graphics2D createGraphics()
          Method to get a Graphics2D object for this picture which can be used to do 2D drawing on the picture
 void drawString(String text, int xPos, int yPos)
          Method to draw a string at the given location on the picture
 void explore()
          Method to open a picture explorer on a copy (in memory) of this simple picture
 int getBasicPixel(int x, int y)
          Method to return the pixel value as an int for the given x and y location
 BufferedImage getBufferedImage()
          Method to get the buffered image
 String getExtension()
          Method to get the extension for this picture
 String getFileName()
          Method to get the file name associated with the picture
 Graphics getGraphics()
          Method to get a graphics object for this picture to use to draw on
 int getHeight()
          Method to get the height of the picture in pixels
 Image getImage()
          Method to get an image from the picture
static String getMediaPath(String fileName)
          Method to get the directory for the media
 PictureFrame getPictureFrame()
          Method to get the picture frame for the picture
 Picture getPictureWithHeight(int height)
          Method to create a new picture of the passed height.
 Picture getPictureWithWidth(int width)
          Method to create a new picture of the passed width.
 Pixel getPixel(int x, int y)
          Method to get a pixel object for the given x and y location
 Pixel[] getPixels()
          Method to get a one-dimensional array of Pixels for this simple picture
 Pixel[][] getPixels2D()
          Method to get a two-dimensional array of Pixels for this simple picture
 String getTitle()
          Method to get the title of the picture
 Rectangle2D getTransformEnclosingRect(AffineTransform trans)
          Method to get the coordinates of the enclosing rectangle after this transformation is applied to the current picture
 Rectangle2D getTranslationEnclosingRect(AffineTransform trans)
          Method to get the coordinates of the enclosing rectangle after this transformation is applied to the current picture
 int getWidth()
          Method to get the width of the picture in pixels
 void hide()
          Method to hide the picture display
 void load(Image image)
          Method to load the buffered image with the passed image
 boolean load(String fileName)
          Method to read the contents of the picture from a filename without throwing errors
 boolean loadImage(String fileName)
          Method to load the picture from the passed file name this just calls load(fileName) and is for name compatibility
 void loadOrFail(String fileName)
          Method to load the picture from the passed file name
 boolean loadPictureAndShowIt(String fileName)
          Method to load a picture from a file name and show it in a picture frame
 void repaint()
          Method to force the picture to repaint itself.
 Picture scale(double rFactor, double cFactor)
          Method to create a new picture by scaling the current picture by the given factors
 void setAllPixelsToAColor(Color color)
          Method to set the color in the picture to the passed color
 void setBasicPixel(int x, int y, int rgb)
          Method to set the value of a pixel in the picture from an int
 void setFileName(String name)
          Method to set the file name
 void setPictureFrame(PictureFrame pictureFrame)
          Method to set the picture frame for this picture
 void setTitle(String title)
          Method to set the title for the picture
 void setVisible(boolean flag)
          Method to make this picture visible or not
 void show()
          Method to show the picture in a picture frame
 String toString()
          Method to return a string with information about this picture
 boolean write(String fileName)
          Method to write the contents of the picture to a file with the passed name without throwing errors
 void writeOrFail(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

SimplePicture

public SimplePicture()
A Constructor that takes no arguments. It creates a picture with a width of 200 and a height of 100 that is all white. A no-argument constructor must be given in order for a class to be able to be subclassed. By default all subclasses will implicitly call this in their parent's no-argument constructor unless a different call to super() is explicitly made as the first line of code in a constructor.


SimplePicture

public SimplePicture(BufferedImage image)
A constructor that takes a buffered image

Parameters:
image - the buffered image

SimplePicture

public SimplePicture(int width,
                     int height)
A constructor that takes the width and height desired for a picture and creates a buffered image of that size. This constructor doesn't show the picture. The pixels will all be white.

Parameters:
width - the desired width
height - the desired height

SimplePicture

public SimplePicture(int width,
                     int height,
                     Color theColor)
A constructor that takes the width and height desired for a picture and creates a buffered image of that size. It also takes the color to use for the background of the picture.

Parameters:
width - the desired width
height - the desired height
theColor - the background color for the picture

SimplePicture

public SimplePicture(SimplePicture copyPicture)
A Constructor that takes a picture to copy information from

Parameters:
copyPicture - the picture to copy from

SimplePicture

public SimplePicture(String fileName)
A Constructor that takes a file name and uses the file to create a picture

Parameters:
fileName - the file name to use in creating the picture
Method Detail

addMessage

public void addMessage(String message,
                       int xPos,
                       int yPos)
Method to draw a message as a string on the buffered image

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

copyPicture

public void copyPicture(SimplePicture sourcePicture)
Method that will copy all of the passed source picture into the current picture object

Parameters:
sourcePicture - the picture object to copy

createGraphics

public Graphics2D createGraphics()
Method to get a Graphics2D object for this picture which can be used to do 2D drawing on the picture


drawString

public void drawString(String text,
                       int xPos,
                       int yPos)
Method to draw a string at the given location on the picture

Parameters:
text - the text to draw
xPos - the left x for the text
yPos - the top y for the text

explore

public void explore()
Method to open a picture explorer on a copy (in memory) of this simple picture

Specified by:
explore in interface DigitalPicture

getBasicPixel

public int getBasicPixel(int x,
                         int y)
Method to return the pixel value as an int for the given x and y location

Specified by:
getBasicPixel in interface DigitalPicture
Parameters:
x - the x coordinate of the pixel
y - the y coordinate of the pixel
Returns:
the pixel value as an integer (alpha, red, green, blue)

getBufferedImage

public BufferedImage getBufferedImage()
Method to get the buffered image

Specified by:
getBufferedImage in interface DigitalPicture
Returns:
the buffered image

getExtension

public String getExtension()
Method to get the extension for this picture

Returns:
the extension (jpg, bmp, giff, etc)

getFileName

public String getFileName()
Method to get the file name associated with the picture

Specified by:
getFileName in interface DigitalPicture
Returns:
the file name associated with the picture

getGraphics

public Graphics getGraphics()
Method to get a graphics object for this picture to use to draw on

Returns:
a graphics object to use for drawing

getHeight

public int getHeight()
Method to get the height of the picture in pixels

Specified by:
getHeight in interface DigitalPicture
Returns:
the height of the picture in pixels

getImage

public Image getImage()
Method to get an image from the picture

Specified by:
getImage in interface DigitalPicture
Returns:
the buffered image since it is an image

getMediaPath

public static String getMediaPath(String fileName)
Method to get the directory for the media

Parameters:
fileName - the base file name to use
Returns:
the full path name by appending the file name to the media directory

getPictureFrame

public PictureFrame getPictureFrame()
Method to get the picture frame for the picture

Returns:
the picture frame associated with this picture (it may be null)

getPictureWithHeight

public Picture 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

getPictureWithWidth

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

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

getPixel

public Pixel getPixel(int x,
                      int y)
Method to get a pixel object for the given x and y location

Specified by:
getPixel in interface DigitalPicture
Parameters:
x - the x location of the pixel in the picture
y - the y location of the pixel in the picture
Returns:
a Pixel object for this location

getPixels

public Pixel[] getPixels()
Method to get a one-dimensional array of Pixels for this simple picture

Specified by:
getPixels in interface DigitalPicture
Returns:
a one-dimensional array of Pixel objects starting with y=0 to y=height-1 and x=0 to x=width-1.

getPixels2D

public Pixel[][] getPixels2D()
Method to get a two-dimensional array of Pixels for this simple picture

Specified by:
getPixels2D in interface DigitalPicture
Returns:
a two-dimensional array of Pixel objects in row-major order.

getTitle

public String getTitle()
Method to get the title of the picture

Specified by:
getTitle in interface DigitalPicture
Returns:
the title of the picture

getTransformEnclosingRect

public Rectangle2D getTransformEnclosingRect(AffineTransform trans)
Method to get the coordinates of the enclosing rectangle after this transformation is applied to the current picture

Returns:
the enclosing rectangle

getTranslationEnclosingRect

public Rectangle2D getTranslationEnclosingRect(AffineTransform trans)
Method to get the coordinates of the enclosing rectangle after this transformation is applied to the current picture

Returns:
the enclosing rectangle

getWidth

public int getWidth()
Method to get the width of the picture in pixels

Specified by:
getWidth in interface DigitalPicture
Returns:
the width of the picture in pixels

hide

public void hide()
Method to hide the picture display


load

public void load(Image image)
Method to load the buffered image with the passed image

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

load

public boolean load(String fileName)
Method to read the contents of the picture from a filename without throwing errors

Specified by:
load in interface DigitalPicture
Parameters:
fileName - the name of the file to write the picture to
Returns:
true if success else false

loadImage

public boolean loadImage(String fileName)
Method to load the picture from the passed file name this just calls load(fileName) and is for name compatibility

Parameters:
fileName - the file name to use to load the picture from
Returns:
true if success else false

loadOrFail

public void loadOrFail(String fileName)
                throws IOException
Method to load the picture from the passed file name

Parameters:
fileName - the file name to use to load the picture from
Throws:
IOException - if the picture isn't found

loadPictureAndShowIt

public boolean loadPictureAndShowIt(String fileName)
Method to load a picture from a file name and show it in a picture frame

Parameters:
fileName - the file name to load the picture from
Returns:
true if success else false

repaint

public void repaint()
Method to force the picture to repaint itself. This is very useful after you have changed the pixels in a picture and you want to see the change.


scale

public Picture scale(double rFactor,
                     double cFactor)
Method to create a new picture by scaling the current picture by the given factors

Parameters:
rFactor - the amount to scale in the height (rows)
cFactor - the amount to scale in the width (columns)
Returns:
the resulting picture

setAllPixelsToAColor

public void setAllPixelsToAColor(Color color)
Method to set the color in the picture to the passed color

Parameters:
color - the color to set to

setBasicPixel

public void setBasicPixel(int x,
                          int y,
                          int rgb)
Method to set the value of a pixel in the picture from an int

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

setFileName

public void setFileName(String name)
Method to set the file name

Parameters:
name - the full pathname of the file

setPictureFrame

public void setPictureFrame(PictureFrame pictureFrame)
Method to set the picture frame for this picture

Parameters:
pictureFrame - the picture frame to use

setTitle

public void setTitle(String title)
Method to set the title for the picture

Specified by:
setTitle in interface DigitalPicture
Parameters:
title - the title to use for the picture

setVisible

public void setVisible(boolean flag)
Method to make this picture visible or not

Parameters:
flag - true if you want it visible else false

show

public void show()
Method to show the picture in a picture frame

Specified by:
show in interface DigitalPicture

toString

public String toString()
Method to return a string with information about this picture

Overrides:
toString in class Object
Returns:
a string with information about the picture

write

public boolean write(String fileName)
Method to write the contents of the picture to a file with the passed name without throwing errors

Specified by:
write in interface DigitalPicture
Parameters:
fileName - the name of the file to write the picture to
Returns:
true if success else false

writeOrFail

public void writeOrFail(String fileName)
                 throws IOException
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
Throws:
IOException