Java Reference
In-Depth Information
The final parameter in the
drawRGB()
method,
processAlpha
, indicates whether the integer
array is considered to contain an alpha (opacity) component. If the parameter is
false
, every
pixel of the image is considered fully opaque. If
processAlpha
is
true
, the opacity of each pixel
is determined by the high-order byte of the integer value, and the pixel's color will be blended
with the drawing surface appropriately. An alpha value of 0 is fully transparent, while an alpha
value of 255 is fully opaque.
Blitting
Blitting
, the copying of one region of the screen to another location, is a crucial operation for
some types of games. There is one method of the
Graphics
class that can be used for blitting:
public void copyArea(int x_src, int y_src, int width, int height,
int x_dest, int y_dest, int anchor)
This method is pretty self-explanatory. It copies a portion of the screen, described by
x_src
,
y_src
,
width
, and
height
, to a destination described by
x_dest
,
y_dest
, and
anchor
. The
anchor works the same as for the
drawImage()
method.
This method works only on a
Graphics
object that does not draw directly to the screen.
A
Graphics
object that draws to an image is fine, as is a
Graphics
object that works on a double-
buffered
Canvas
. A
Graphics
object from
GameCanvas
's
getGraphics()
method will also work. By
contrast, a
Graphics
object for a non-double-buffered
Canvas
will throw an
IllegalStateException
if the
copyArea()
method is called. (See the upcoming section on double buffering for more
information on the technique.)
Clipping
Graphics
maintains a rectangular
clipping shape
. The clipping shape limits drawing, such that
any drawing that takes place outside of the clipping shape will not be displayed. It's kind of like
painting through a stencil, except you can only use a rectangular stencil. If you were writing a
game that had some kind of border on the game board, you might set the clipping rectangle to
be the inside of the game board, so that no drawing could overwrite the border.
You can find out the current clipping rectangle by calling
getClipX()
,
getClipY()
,
getClipWidth()
, and
getClipHeight()
.
If you would like to modify the clipping rectangle, there are two methods that you can use.
First, you can set the clipping rectangle directly by calling the following method:
public void setClip(int x, int y, int width, int height);
The other possibility is to limit the current clipping rectangle with another rectangle. The
following method takes the intersection of the current clipping rectangle and the supplied
rectangle and uses it to set the new clipping rectangle:
public void clipRect(int x, int y, int width, int height);
Key Events
Canvas
handles events at a lower level than the other
Displayable
subclasses. Although you can
add
Command
s and respond to them,
Canvas
also includes a set of methods that handle interaction
with the individual keys of a device.
Search WWH ::
Custom Search