objectdraw
Class VisibleImage

java.lang.Object
  extended by objectdraw.ObjectDrawObject
      extended by objectdraw.ObjectDrawShape
          extended by objectdraw.ObjectDrawRectangularShape
              extended by objectdraw.VisibleImage
All Implemented Interfaces:
objectdraw.Dependent, Drawable2DInterface, DrawableInterface, LocatableInterface, Resizable2DInterface

public class VisibleImage
extends objectdraw.ObjectDrawRectangularShape

A VisibleImage is an image that can be placed on a canvas. To construct it you must retrieve an Image object, most likely using the getImage() method of Controller. Like other shapes that implement the Drawable2DInterface, the shape of the image as it is drawn on the canvas is defined by a top left corner, plus a width and height. However, it is also possible to retrieve the actual dimensions of the image, or to not specify the width and height for the image and thus allow it to take its natural size.

Author:
Russell Zahniser (russell@zahniser.net)

Constructor Summary
VisibleImage(Image image, double x, double y, double width, double height, DrawingCanvas canvas)
          Construct a VisibleImage to display the given image, with its top left corner at (x, y) and scaled to fit in a rectangle of the given width and height.
VisibleImage(Image image, double x, double y, DrawingCanvas canvas)
          Construct a VisibleImage to display the given image, with its top left corner at (x, y), drawn the same size as in the image file.
VisibleImage(Image image, Location point, double width, double height, DrawingCanvas canvas)
          Construct a VisibleImage to display the given image, with its top left corner at point and scaled to fit in a rectangle of the given width and height.
VisibleImage(Image image, Location point, DrawingCanvas canvas)
          Construct a VisibleImage to display the given image, with its top left corner at point, drawn the same size as in the image file.
VisibleImage(Image image, Location corner1, Location corner2, DrawingCanvas canvas)
          Construct a VisibleImage to display the given image, stretching between the points corner1 and corner2.
 
Method Summary
 void addToCanvas(DrawingCanvas c)
          Place this object on the specified canvas.
 boolean contains(Location point)
          Return true if the given location is inside this object, false otherwise.
static BufferedImage createBufferedCopy(Image image)
          Return a copy of this Image as a BufferedImage.
 DrawingCanvas getCanvas()
          Return the canvas that this object is on.
 Color getColor()
          Return the color of this object.
 double getDoubleHeight()
          Return the height of the rectangle bounding this object, in double precision.
 double getDoubleWidth()
          Return the height of the rectangle bounding this object, in double precision.
 double getDoubleX()
          Get this object's x coordinate, in double precision.
 double getDoubleY()
          Get this object's y coordinate, in double precision.
 int getHeight()
          Return the width of the rectangle bounding this object.
 Image getImage()
          Get the Image object belonging to this VisibleImage.
 int getImageHeight()
          Get the actual height in pixels of the image file that this VisibleImage draws.
 int getImageWidth()
          Get the actual width in pixels of the image file that this VisibleImage draws.
 Location getLocation()
          Get this object's location.
 int getWidth()
          Return the height of the rectangle bounding this object.
 int getX()
          Get this object's x coordinate.
 int getY()
          Get this object's y coordinate.
 void hide()
          Make this object invisible.
 boolean isHidden()
          Return true if this object has been rendered invisible with a call to its hide() method, false if it is still being drawn.
 void move(double dx, double dy)
          Shift (translate) this object left or right by an amount dx, and up or down by an amount dy.
 void moveTo(double x, double y)
          Move the reference point of this object to the given location.
 void moveTo(Location point)
          Move the reference point of this object to the given location.
 boolean overlaps(Drawable2DInterface other)
          Return true if the rectangle bounding this object overlaps with the rectangle bounding another object; false otherwise.
 void removeFromCanvas()
          Remove this object from its canvas.
 void sendBackward()
          Move this object one step backward in the draw order, causing it to be drawn underneath the objects that are in front of it.
 void sendForward()
          Move this object one step forward in the draw order, causing it to be drawn on top of the objects that are behind it.
 void sendToBack()
          Move this object to the very back of the draw order, causing it to be drawn underneath all the other objects.
 void sendToFront()
          Move this object to the very front of the draw order, causing it to be drawn on top of all the other objects.
 void setColor(Color c)
          Change the color of this object.
 void setHeight(double h)
          Set the height of this object, leaving its top edge in place and stretching the bottom.
 void setImage(Image image)
          Change the Image object belonging to this VisibleImage.
 void setSize(double width, double height)
          Set the width and height of this object.
 void setWidth(double w)
          Set the width of this object, leaving its left edge in place and stretching the right.
 void show()
          Make an object visible again after it had been hidden.
 String toString()
          Return a string describing how this object might be constructed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

VisibleImage

public VisibleImage(Image image,
                    double x,
                    double y,
                    double width,
                    double height,
                    DrawingCanvas canvas)
Construct a VisibleImage to display the given image, with its top left corner at (x, y) and scaled to fit in a rectangle of the given width and height.

Parameters:
image - Image to be displayed
x - x coordinate of the top left corner
y - y coordinate of the top left corner
width - width to which the image should be scaled
height - height to which the image should be scaled
canvas - DrawingCanvas on which to place this image

VisibleImage

public VisibleImage(Image image,
                    double x,
                    double y,
                    DrawingCanvas canvas)
Construct a VisibleImage to display the given image, with its top left corner at (x, y), drawn the same size as in the image file.

Parameters:
image - Image to be displayed
x - x coordinate of the top left corner
y - y coordinate of the top left corner
canvas - DrawingCanvas on which to place this image

VisibleImage

public VisibleImage(Image image,
                    Location point,
                    double width,
                    double height,
                    DrawingCanvas canvas)
Construct a VisibleImage to display the given image, with its top left corner at point and scaled to fit in a rectangle of the given width and height.

Parameters:
image - Image to be displayed
point - Location of the top left corner
width - width to which the image should be scaled
height - height to which the image should be scaled
canvas - DrawingCanvas on which to place this image

VisibleImage

public VisibleImage(Image image,
                    Location point,
                    DrawingCanvas canvas)
Construct a VisibleImage to display the given image, with its top left corner at point, drawn the same size as in the image file.

Parameters:
image - Image to be displayed
point - Location of the top left corner
canvas - DrawingCanvas on which to place this image

VisibleImage

public VisibleImage(Image image,
                    Location corner1,
                    Location corner2,
                    DrawingCanvas canvas)
Construct a VisibleImage to display the given image, stretching between the points corner1 and corner2.

Parameters:
image - Image to be displayed
corner1 - Location of one corner
corner2 - Location of the opposite corner
canvas - DrawingCanvas on which to place this image
Method Detail

addToCanvas

public void addToCanvas(DrawingCanvas c)
Place this object on the specified canvas. The object will be removed from its current canvas, if it is on a canvas that is not the one it is being moved to. The object will be at the front of the canvas, as if it had just been drawn. If the given canvas is null the object is simply removed from its current canvas.

Specified by:
addToCanvas in interface DrawableInterface
Overrides:
addToCanvas in class objectdraw.ObjectDrawShape
Parameters:
c - DrawingCanvas to place this object on

contains

public boolean contains(Location point)
Return true if the given location is inside this object, false otherwise. Because Lines and FramedArcs have no "inside", a point is considered to be contained in one of them as long as it is within a few pixels of the line.

Specified by:
contains in interface DrawableInterface
Overrides:
contains in class objectdraw.ObjectDrawRectangularShape
Parameters:
point - Location to test
Returns:
whether the given location is inside this shape

createBufferedCopy

public static BufferedImage createBufferedCopy(Image image)
Return a copy of this Image as a BufferedImage. This allows you to easily manipulate the actual pixel data that makes up that image. A typical usage would be to replace the image of a VisibleImage with a buffered copy so that you can then manipulate its pixels:

Image image = getImage("picture.jpg");
BufferedImage buffer = VisibleImage.createBufferedCopy(image);
new VisibleImage(image, 0, 0, canvas);

Returns:
a BufferedImage copy of the given Image.

getCanvas

public DrawingCanvas getCanvas()
Return the canvas that this object is on. If it has been removed from its canvas, this method will return null.

Specified by:
getCanvas in interface DrawableInterface
Overrides:
getCanvas in class objectdraw.ObjectDrawShape
Returns:
the DrawingCanvas that this object is on.

getColor

public Color getColor()
Return the color of this object. This attribute has no meaning for an image, but it is included because all other drawable objects do have a color.

Specified by:
getColor in interface DrawableInterface
Overrides:
getColor in class objectdraw.ObjectDrawShape
Returns:
the Color of this object

getDoubleHeight

public double getDoubleHeight()
Return the height of the rectangle bounding this object, in double precision.

Specified by:
getDoubleHeight in interface Drawable2DInterface
Overrides:
getDoubleHeight in class objectdraw.ObjectDrawRectangularShape
Returns:
the height of the rectangle bounding this object, in double precision.

getDoubleWidth

public double getDoubleWidth()
Return the height of the rectangle bounding this object, in double precision.

Specified by:
getDoubleWidth in interface Drawable2DInterface
Overrides:
getDoubleWidth in class objectdraw.ObjectDrawRectangularShape
Returns:
the height of the rectangle bounding this object, in double precision.

getDoubleX

public double getDoubleX()
Get this object's x coordinate, in double precision.

Specified by:
getDoubleX in interface LocatableInterface
Overrides:
getDoubleX in class objectdraw.ObjectDrawRectangularShape
Returns:
this object's x coordinate, in double precision

getDoubleY

public double getDoubleY()
Get this object's y coordinate, in double precision.

Specified by:
getDoubleY in interface LocatableInterface
Overrides:
getDoubleY in class objectdraw.ObjectDrawRectangularShape
Returns:
this object's y coordinate, in double precision

getHeight

public int getHeight()
Return the width of the rectangle bounding this object.

Specified by:
getHeight in interface Drawable2DInterface
Overrides:
getHeight in class objectdraw.ObjectDrawRectangularShape
Returns:
the width of the rectangle bounding this object.

getImage

public Image getImage()
Get the Image object belonging to this VisibleImage.

Returns:
the Image object belonging to this VisibleImage

getImageHeight

public int getImageHeight()
Get the actual height in pixels of the image file that this VisibleImage draws. This might not be the same as getHeight(), in which case the image is being scaled when it is drawn.

Returns:
the natural height of this image

getImageWidth

public int getImageWidth()
Get the actual width in pixels of the image file that this VisibleImage draws. This might not be the same as getWidth(), in which case the image is being scaled when it is drawn.

Returns:
the natural width of this image

getLocation

public Location getLocation()
Get this object's location. Translating this Location object will have the same effect as moving the object. So, the following two lines of code are equivalent:

object.move(10, 20);
object.getLocation().translate(10, 20);


If you want to create a Location offset by some amount from the Location of this object, without actually moving this object, use the offset() method in Location, which creates a new Location.

Specified by:
getLocation in interface LocatableInterface
Overrides:
getLocation in class objectdraw.ObjectDrawRectangularShape
Returns:
the Location of this object.

getWidth

public int getWidth()
Return the height of the rectangle bounding this object.

Specified by:
getWidth in interface Drawable2DInterface
Overrides:
getWidth in class objectdraw.ObjectDrawRectangularShape
Returns:
the height of the rectangle bounding this object.

getX

public int getX()
Get this object's x coordinate.

Specified by:
getX in interface LocatableInterface
Overrides:
getX in class objectdraw.ObjectDrawRectangularShape
Returns:
this object's x coordinate

getY

public int getY()
Get this object's y coordinate.

Specified by:
getY in interface LocatableInterface
Overrides:
getY in class objectdraw.ObjectDrawRectangularShape
Returns:
this object's y coordinate

hide

public void hide()
Make this object invisible. It stays in position on its canvas; it is simply not drawn. This method has no effect if the object was already hidden.

Specified by:
hide in interface DrawableInterface
Overrides:
hide in class objectdraw.ObjectDrawShape

isHidden

public boolean isHidden()
Return true if this object has been rendered invisible with a call to its hide() method, false if it is still being drawn.

Specified by:
isHidden in interface DrawableInterface
Overrides:
isHidden in class objectdraw.ObjectDrawShape
Returns:
whether this object is hidden

move

public void move(double dx,
                 double dy)
Shift (translate) this object left or right by an amount dx, and up or down by an amount dy. A negative dx indicates a shift to the left; a negative dy indicates a shift up

Specified by:
move in interface DrawableInterface
Overrides:
move in class objectdraw.ObjectDrawShape
Parameters:
dx - amount of x translation
dy - amount of y translation

moveTo

public void moveTo(double x,
                   double y)
Move the reference point of this object to the given location. For a Drawable2D object, the reference point is the top left corner; for a line, it is the start point.

Specified by:
moveTo in interface DrawableInterface
Overrides:
moveTo in class objectdraw.ObjectDrawShape
Parameters:
x - x coordinate to move to
y - y coordinate to move to

moveTo

public void moveTo(Location point)
Move the reference point of this object to the given location. For a Drawable2D object, the reference point is the top left corner; for a line, it is the start point.

Specified by:
moveTo in interface DrawableInterface
Overrides:
moveTo in class objectdraw.ObjectDrawShape
Parameters:
point - Location to move to

overlaps

public boolean overlaps(Drawable2DInterface other)
Return true if the rectangle bounding this object overlaps with the rectangle bounding another object; false otherwise.

Specified by:
overlaps in interface Drawable2DInterface
Overrides:
overlaps in class objectdraw.ObjectDrawRectangularShape
Parameters:
other - another two dimensional object to check for overlapping with
Returns:
whether the bounding rectangles of the two objects overlap

removeFromCanvas

public void removeFromCanvas()
Remove this object from its canvas.

Specified by:
removeFromCanvas in interface DrawableInterface
Overrides:
removeFromCanvas in class objectdraw.ObjectDrawShape

sendBackward

public void sendBackward()
Move this object one step backward in the draw order, causing it to be drawn underneath the objects that are in front of it.

Specified by:
sendBackward in interface DrawableInterface
Overrides:
sendBackward in class objectdraw.ObjectDrawShape

sendForward

public void sendForward()
Move this object one step forward in the draw order, causing it to be drawn on top of the objects that are behind it.

Specified by:
sendForward in interface DrawableInterface
Overrides:
sendForward in class objectdraw.ObjectDrawShape

sendToBack

public void sendToBack()
Move this object to the very back of the draw order, causing it to be drawn underneath all the other objects.

Specified by:
sendToBack in interface DrawableInterface
Overrides:
sendToBack in class objectdraw.ObjectDrawShape

sendToFront

public void sendToFront()
Move this object to the very front of the draw order, causing it to be drawn on top of all the other objects.

Specified by:
sendToFront in interface DrawableInterface
Overrides:
sendToFront in class objectdraw.ObjectDrawShape

setColor

public void setColor(Color c)
Change the color of this object. This attribute has no meaning for an image, but it is included because all other drawable objects do have a color.

Specified by:
setColor in interface DrawableInterface
Overrides:
setColor in class objectdraw.ObjectDrawShape
Parameters:
c - new Color for this object

setHeight

public void setHeight(double h)
Set the height of this object, leaving its top edge in place and stretching the bottom.

Specified by:
setHeight in interface Resizable2DInterface
Overrides:
setHeight in class objectdraw.ObjectDrawRectangularShape
Parameters:
h - new height

setImage

public void setImage(Image image)
Change the Image object belonging to this VisibleImage.

Parameters:
image - new Image object

setSize

public void setSize(double width,
                    double height)
Set the width and height of this object. Its upper left corner will stay fixed in place, and the opposite corner will move to make it the new size.

Specified by:
setSize in interface Resizable2DInterface
Overrides:
setSize in class objectdraw.ObjectDrawRectangularShape
Parameters:
width - new width
height - new height

setWidth

public void setWidth(double w)
Set the width of this object, leaving its left edge in place and stretching the right.

Specified by:
setWidth in interface Resizable2DInterface
Overrides:
setWidth in class objectdraw.ObjectDrawRectangularShape
Parameters:
w - new width

show

public void show()
Make an object visible again after it had been hidden. This method has no effect if the object was already visible.

Specified by:
show in interface DrawableInterface
Overrides:
show in class objectdraw.ObjectDrawShape

toString

public String toString()
Return a string describing how this object might be constructed.

Overrides:
toString in class objectdraw.ObjectDrawObject