acm.graphics
Class GImage

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GImage

public class GImage
extends GObject
implements GResizable, GScalable

The GImage class is a graphical object whose appearance is defined by an image.


Constructor Summary
GImage(Image image)
Creates a new GImage object at the origin that displays the specified image.
GImage(Image image, double x, double y)
Creates a new GImage object at the specified coordinates.
GImage(int[][] array)
Creates a new GImage object at the origin.
GImage(int[][] array, double x, double y)
Creates a new GImage object at the specified coordinates.
GImage(String name)
Creates a new GImage object by looking for an image with that name.
GImage(String name, double x, double y)
Creates a new GImage object at the specified coordinates.
 
Method Summary
int createRGBPixel(int red, int green, int blue)
Creates an opaque pixel value with the color components given by red, green, and blue.
int createRGBPixel(int red, int green, int blue, int alpha)
Creates a pixel value with the color components given by red, green, and blue and the transparency value alpha.
int getAlpha(int pixel)
Returns the alpha component from an RGB value.
int getBlue(int pixel)
Returns the blue component from an RGB value.
 GRectangle getBounds()
Returns the bounding box of this object.
int getGreen(int pixel)
Returns the green component from an RGB value.
 Image getImage()
Returns the image stored inside this GImage.
 int[][] getPixelArray()
Returns a two-dimensional array of pixel values from the stored image.
int getRed(int pixel)
Returns the red component from an RGB value.
 GDimension getSize()
Returns the size of this object as a GDimension.
 void saveImage(File file)
Saves the image to the specified file.
 void saveImage(String filename)
Saves the image to a file with the specified filename.
 void scale(double sf)
Scales the object on the screen by the scale factor sf, which applies in both dimensions.
 void scale(double sx, double sy)
Scales the object on the screen by the scale factors sx and sy.
 void setBounds(double x, double y, double width, double height)
Changes the bounds of this object to the specified values.
 void setBounds(GRectangle bounds)
Changes the bounds of this object to the values from the specified GRectangle.
 void setImage(Image image)
Resets the image used by this GImage object to the new image specified as an argument.
 void setImage(String name)
Resets the image used by this GImage object to the one identified by the argument name, which is processed exactly as described in the constructors.
 void setSize(double width, double height)
Changes the size of this object to the specified width and height.
 
Inherited Method Summary
voidaddMouseListener(MouseListener listener)
Adds a mouse listener to this graphical object.
voidaddMouseMotionListener(MouseMotionListener listener)
Adds a mouse motion listener to this graphical object.
booleancontains(GPoint pt)
Checks to see whether a point is inside the object.
booleancontains(double x, double y)
Checks to see whether a point is inside the object.
doublegetHeight()
Returns the height of this object as a double-precision value, which is defined to be the height of the bounding box.
GPointgetLocation()
Returns the location of this object as a GPoint.
doublegetWidth()
Returns the width of this object as a double-precision value, which is defined to be the width of the bounding box.
doublegetX()
Returns the x-coordinate of the object.
doublegetY()
Returns the y-coordinate of the object.
booleanisVisible()
Checks to see whether this object is visible.
voidmove(double dx, double dy)
Moves the object on the screen using the displacements dx and dy.
voidmovePolar(double r, double theta)
Moves the object using displacements given in polar coordinates.
voidremoveMouseListener(MouseListener listener)
Removes a mouse listener from this graphical object.
voidremoveMouseMotionListener(MouseMotionListener listener)
Removes a mouse motion listener from this graphical object.
voidsendBackward()
Moves this object one step toward the back in the z dimension.
voidsendForward()
Moves this object one step toward the front in the z dimension.
voidsendToBack()
Moves this object to the back of the display in the z dimension.
voidsendToFront()
Moves this object to the front of the display in the z dimension.
voidsetLocation(GPoint pt)
Sets the location of this object to the specified point.
voidsetLocation(double x, double y)
Sets the location of this object to the point (x, y).
voidsetVisible(boolean visible)
Sets whether this object is visible.
 

Constructor Detail

public GImage(Image image)

Creates a new GImage object at the origin that displays the specified image.

 
Usage: GImage gimage = new GImage(image); 
Parameter: 
image  The image to use as the contents of this GImage
 


public GImage(Image image, double x, double y)

Creates a new GImage object at the specified coordinates. The image parameter is used to initialize the appearance of the image.

 
Usage: GImage gimage = new GImage(image, x, y); 
Parameters: 
image  The image to use as the contents of this GImage
 The x-coordinate of the upper left corner of the image
 The y-coordinate of the upper left corner of the image
 


public GImage(int[][] array)

Creates a new GImage object at the origin. The array parameter is a two-dimensional pixel array in which each pixel value consists of an integer that is subdivided into four eight-bit bytes, as follows:
alpha << 24 | red << 16 | green << 8 | blue
The alpha value indicates the transparency, and the other values are the red, green, and blue components of the color.

 
Usage: GImage gimage = new GImage(array); 
Parameter: 
array  A two-dimensional pixel array
 


public GImage(int[][] array, double x, double y)

Creates a new GImage object at the specified coordinates. The array parameter is a two-dimensional pixel array in which each pixel value consists of an integer that is subdivided into four eight-bit bytes, as follows:
alpha << 24 | red << 16 | green << 8 | blue
The alpha value indicates the transparency, and the other values are the red, green, and blue components of the color.

 
Usage: GImage gimage = new GImage(array, x, y); 
Parameters: 
array  A two-dimensional pixel array
 The x-coordinate of the upper left corner of the image
 The y-coordinate of the upper left corner of the image
 


public GImage(String name)

Creates a new GImage object by looking for an image with that name. The search for an image by name consists of the following steps:

  1. Check to see if an image with that name has already been defined. If so, return that image.

  2. Check to see if there is a resource available with that name whose contents can be read as an Image. If so, read the image from the resource file.

  3. Load the image from a file with the specified name, relative to the application directory or the applet code base.


 
Usage: GImage gimage = new GImage(name); 
Parameter: 
name  The name used to search for the contents of this image
 


public GImage(String name, double x, double y)

Creates a new GImage object at the specified coordinates. The name parameter is used to identify an image to display, as described in the single-argument version of the GImage constructor.

 
Usage: GImage gimage = new GImage(image, x, y); 
Parameters: 
name  The name used to search for the contents of this image
 The x-coordinate of the upper left corner of the image
 The y-coordinate of the upper left corner of the image
 

Method Detail

public static int createRGBPixel(int red, int green, int blue)

Creates an opaque pixel value with the color components given by red, green, and blue.

 
Usage: int pixel = GImage.createRGBPixel(red, green, blue); 
Parameters: 
red  The red component of the pixel (0 to 255)
green  The green component of the pixel (0 to 255)
blue  The blue component of the pixel (0 to 255)
Returns: An opaque pixel value containing these components
 


public static int createRGBPixel(int red, int green, int blue, int alpha)

Creates a pixel value with the color components given by red, green, and blue and the transparency value alpha.

 
Usage: int pixel = GImage.createRGBPixel(red, green, blue); 
Parameters: 
red  The red component of the pixel (0 to 255)
green  The green component of the pixel (0 to 255)
blue  The blue component of the pixel (0 to 255)
alpha  The transparency value of the pixel (0 to 255)
Returns: A pixel value containing these components
 


public static int getAlpha(int pixel)

Returns the alpha component from an RGB value.

 
Usage: int alpha = GImage.getAlpha(pixel); 
Parameter: 
pixel  An int containing a pixel value as alpha/red/green/blue.
Returns: The alpha component of the pixel
 


public static int getBlue(int pixel)

Returns the blue component from an RGB value.

 
Usage: int blue = GImage.getBlue(pixel); 
Parameter: 
pixel  An int containing a pixel value as alpha/red/green/blue.
Returns: The blue component of the pixel
 


public GRectangle getBounds()

Returns the bounding box of this object.

 
Usage: GRectangle bounds = gimage.getBounds(); 
Returns: The bounding box for this object
 

Specified by:
getBounds in class GObject

public static int getGreen(int pixel)

Returns the green component from an RGB value.

 
Usage: int green = GImage.getGreen(pixel); 
Parameter: 
pixel  An int containing a pixel value as alpha/red/green/blue.
Returns: The green component of the pixel
 


public Image getImage()

Returns the image stored inside this GImage.

 
Usage: Image image = gimage.getImage(); 
Returns: The Image object stored inside this GImage
 


public int[][] getPixelArray()

Returns a two-dimensional array of pixel values from the stored image.

 
Usage: int[][] array = gimage.getPixelArray(); 
Returns: A two-dimensional array of pixel values from the stored image
 


public static int getRed(int pixel)

Returns the red component from an RGB value.

 
Usage: int red = GImage.getRed(pixel); 
Parameter: 
pixel  An int containing a pixel value as alpha/red/green/blue.
Returns: The red component of the pixel
 


public GDimension getSize()

Returns the size of this object as a GDimension.

 
Usage: GDimension size = gimage.getSize(); 
Returns: The size of this object
 


public void saveImage(File file)

Saves the image to the specified file. The data format for the image file is determined by the suffix of the filename. If the suffix of the file is not recognized as a supported image type, calling this method generates an error.

 
Usage: gimage.saveImage(file); 
Parameter: 
file  The File to which the image is saved
 


public void saveImage(String filename)

Saves the image to a file with the specified filename. The data format for the image file is determined by the suffix of the filename. If the suffix of the file is not recognized as a supported image type, calling this method generates an error.

 
Usage: gimage.saveImage(filename); 
Parameter: 
filename  The name of the file to which the image is saved
 


public final void scale(double sf)

Scales the object on the screen by the scale factor sf, which applies in both dimensions.

 
Usage: gobj.scale(sf); 
Parameter: 
sf  The factor used to scale all coordinates in both dimensions
 

Specified by:
scale in interface GScalable

public void scale(double sx, double sy)

Scales the object on the screen by the scale factors sx and sy.

 
Usage: gobj.scale(sx, sy); 
Parameters: 
sx  The factor used to scale all coordinates in the x direction
sy  The factor used to scale all coordinates in the y direction
 

Specified by:
scale in interface GScalable

public void setBounds(double x, double y, double width, double height)

Changes the bounds of this object to the specified values.

 
Usage: gimage.setBounds(x, y, width, height); 
Parameters: 
 The new x-coordinate for the object
 The new y-coordinate for the object
width  The new width of the object
height  The new height of the object
 

Specified by:
setBounds in interface GResizable

public final void setBounds(GRectangle bounds)

Changes the bounds of this object to the values from the specified GRectangle.

 
Usage: gimage.setBounds(bounds); 
Parameter: 
bounds  A GRectangle specifying the new bounds
 

Specified by:
setBounds in interface GResizable

public void setImage(Image image)

Resets the image used by this GImage object to the new image specified as an argument. Calling setImage automatically changes the size of the image to be equal to that of the image data.

 
Usage: gimage.setImage(image); 
Parameter: 
image  The image to use as the contents of this GImage
 


public void setImage(String name)

Resets the image used by this GImage object to the one identified by the argument name, which is processed exactly as described in the constructors. Calling setImage automatically changes the size of the image to be equal to that of the image data.

 
Usage: gimage.setImage(name); 
Parameter: 
name  The name used to search for the contents of this image
 


public void setSize(double width, double height)

Changes the size of this object to the specified width and height.

 
Usage: gimage.setSize(width, height); 
Parameters: 
width  The new width of the object
height  The new height of the object
 

Specified by:
setSize in interface GResizable