public class GImage extends GObject implements GResizable, GScalable
GImage
class is a graphical object whose appearance is
defined by an image.Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
static int |
createRGBPixel(int red,
int green,
int blue)
Creates an opaque pixel value with the color components given by
red , green , and blue . |
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 . |
void |
fit(double width,
double height)
Resizes the image to the largest size that fits within the given width/height
while maintaining the image's "aspect ratio" (not stretching the image's
relative width/height).
|
static int |
getAlpha(int pixel)
Returns the alpha component from an RGB value.
|
protected Rectangle |
getAWTBounds()
Returns an AWT
Rectangle that specifies the bounds of this object. |
static int |
getBlue(int pixel)
Returns the blue component from an RGB value.
|
GRectangle |
getBounds()
Returns the bounding box of this object.
|
static 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.
|
static int |
getRed(int pixel)
Returns the red component from an RGB value.
|
acm.graphics.GDimension |
getSize()
Returns the size of this object as a
GDimension . |
boolean |
inBounds(double x,
double y)
Returns whether the given x/y pixel position is within the bounds of the image,
in other words, whether it is between (0, 0) and (w, h).
|
protected void |
paint2d(Graphics2D graphics2d)
Implements the
paint2d operation for this graphical object. |
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 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 |
setPixelArray(int[][] pixels)
Sets the image to use the given two-dimensional array of pixel values.
|
void |
setSize(double width,
double height)
Changes the size of this object to the specified width and height.
|
void |
setSize(acm.graphics.GDimension size)
Changes the size of this object to the specified
GDimension . |
addActionListener, addMouseListener, addMouseMotionListener, areMouseListenersEnabled, clone, colorName, colorName, colorNameFriendly, contains, contains, createTransformedGraphics, fireActionEvent, fireActionEvent, fireMouseListeners, getBottomY, getCenterLocation, getCenterX, getCenterY, getColor, getComponent, getHeight, getLineWidth, getLocation, getMatrix, getObjectColor, getParent, getRightX, getWidth, getX, getY, intersects, isAntiAliasing, isVisible, move, movePolar, paint, paramString, pause, removeActionListener, removeMouseListener, removeMouseMotionListener, repaint, rotate, scale, sendBackward, sendForward, sendToBack, sendToFront, setAntiAliasing, setBottomY, setCenterLocation, setCenterLocation, setCenterX, setCenterY, setColor, setLineWidth, setLocation, setLocation, setParent, setRightX, setVisible, setX, setY, start, start, toString, updateEnabledList
public GImage(Image image)
GImage
object at the origin that displays
the specified image.image
- The image to use as the contents of this GImage
public GImage(String name)
GImage
object by looking for an image with that
name. The search for an image by name consists of the following steps:
Image
. If so, read the image
from the resource file.
name
- The name used to search for the contents of this imagepublic GImage(int[][] array)
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:
<<
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.
array
- A two-dimensional pixel arraypublic GImage(Image image, double x, double y)
GImage
object at the specified coordinates. The
image
parameter is used to initialize the appearance of the image.image
- The image to use as the contents of this GImage
x
- The x-coordinate of the upper left corner of the imagey
- The y-coordinate of the upper left corner of the imagepublic GImage(String name, double x, double y)
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.name
- The name used to search for the contents of this imagex
- The x-coordinate of the upper left corner of the imagey
- The y-coordinate of the upper left corner of the imagepublic GImage(int[][] array, double x, double y)
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:
<<
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.
array
- A two-dimensional pixel arrayx
- The x-coordinate of the upper left corner of the imagey
- The y-coordinate of the upper left corner of the imagepublic static int createRGBPixel(int red, int green, int blue)
red
, green
, and blue
.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)public static int createRGBPixel(int red, int green, int blue, int alpha)
red
, green
, and blue
and the transparency value alpha
.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)public void fit(double width, double height)
width
- maximum width, in pixelsheight
- maximum height, in pixelspublic static int getAlpha(int pixel)
pixel
- An int
containing a pixel value as alpha/red/green/blue.protected Rectangle getAWTBounds()
Rectangle
that specifies the bounds of this object.Rectangle
that specifies the bounds of this objectpublic static int getBlue(int pixel)
pixel
- An int
containing a pixel value as alpha/red/green/blue.public GRectangle getBounds()
public static int getGreen(int pixel)
pixel
- An int
containing a pixel value as alpha/red/green/blue.public Image getImage()
GImage
.Image
object stored inside this GImage
public static int getRed(int pixel)
pixel
- An int
containing a pixel value as alpha/red/green/blue.public acm.graphics.GDimension getSize()
GDimension
.public boolean inBounds(double x, double y)
x
- the pixel's x-coordinatey
- the pixel's y-coordinateprotected void paint2d(Graphics2D graphics2d)
paint2d
operation for this graphical object. This method
is not called directly by clients.public void saveImage(File file)
file
- The File
to which the image is savedpublic void saveImage(String filename)
filename
- The name of the file to which the image is savedpublic void setBounds(double x, double y, double width, double height)
setBounds
in interface GResizable
x
- The new x-coordinate for the objecty
- The new y-coordinate for the objectwidth
- The new width of the objectheight
- The new height of the objectpublic final void setBounds(GRectangle bounds)
GRectangle
.setBounds
in interface GResizable
bounds
- A GRectangle
specifying the new boundspublic void setImage(Image image)
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.image
- The image to use as the contents of this GImage
public void setImage(String name)
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.name
- The name used to search for the contents of this imagepublic void setSize(double width, double height)
setSize
in interface GResizable
width
- The new width of the objectheight
- The new height of the objectpublic final void setSize(acm.graphics.GDimension size)
GDimension
.setSize
in interface GResizable
size
- A GDimension
object specifying the sizepublic void scale(double sx, double sy)
sx
and sy
.public int[][] getPixelArray()
public void setPixelArray(int[][] pixels)