Graphicspublic abstract class Graphics extends Object The abstract Graphics class allows applications to draw on a screen or other
rendering target. There are several properties which define rendering
options: origin point, clipping area, color, font.
The origin point specifies the beginning of the clipping area coordinate
system. All coordinates used in rendering operations are computed with
respect to this point. The clipping area defines the boundaries where
rendering operations can be performed. Rendering operations can't modify
pixels outside of the clipping area.
The draw and fill methods allow applications to drawing shapes, text, images
with specified font and color options in the specified part of the screen. |
Constructors Summary |
---|
protected Graphics()Instantiates a new Graphics. This constructor is default for Graphics and
can not be called directly.
|
Methods Summary |
---|
public abstract void | clearRect(int x, int y, int width, int height)Clears the specified rectangle. This method fills specified rectangle
with background color.
| public abstract void | clipRect(int x, int y, int width, int height)Intersects the current clipping area with a new rectangle. If the current
clipping area is not defined, the rectangle becomes a new clipping area.
Rendering operations are only allowed within the new the clipping area.
| public abstract void | copyArea(int sx, int sy, int width, int height, int dx, int dy)Copies the rectangle area to another area specified by a distance (dx,
dy) from the original rectangle's location. Positive dx and dy values
give a new location defined by translation to the right and down from the
original location, negative dx and dy values - to the left and up.
| public abstract java.awt.Graphics | create()Creates a new copy of this Graphics.
| public java.awt.Graphics | create(int x, int y, int width, int height)Creates a copy of the Graphics object with a new origin and a new
specified clip area. The new clip area is the rectangle defined by the
origin point with coordinates X,Y and the given width and height. The
coordinates of all subsequent rendering operations will be computed with
respect to the new origin and can be performed only within the range of
the clipping area dimensions.
Graphics res = create();
res.translate(x, y);
res.clipRect(0, 0, width, height);
return res;
| public abstract void | dispose()Disposes of the Graphics. This Graphics object can not be used after
calling this method.
| public void | draw3DRect(int x, int y, int width, int height, boolean raised)Draws the highlighted outline of a rectangle.
// Note: lighter/darker colors should be used to draw 3d rect.
// The resulting rect is (width+1)x(height+1). Stroke and paint
// attributes of
// the Graphics2D should be reset to the default values.
// fillRect is used instead of drawLine to bypass stroke
// reset/set and rasterization.
Color color = getColor();
Color colorUp, colorDown;
if (raised) {
colorUp = color.brighter();
colorDown = color.darker();
} else {
colorUp = color.darker();
colorDown = color.brighter();
}
setColor(colorUp);
fillRect(x, y, width, 1);
fillRect(x, y + 1, 1, height);
setColor(colorDown);
fillRect(x + width, y, 1, height);
fillRect(x + 1, y + height, width, 1);
| public abstract void | drawArc(int x, int y, int width, int height, int sa, int ea)Draws the arc covering the specified rectangle and using the current
color. The rectangle is defined by the origin point (X, Y) and dimensions
(width and height). The arc center is the the center of specified
rectangle. The angle origin is 3 o'clock position, the positive angle is
counted as a counter-clockwise rotation, the negative angle is counted as
clockwise rotation.
| public void | drawBytes(byte[] bytes, int off, int len, int x, int y)Draws the text represented by byte array. This method uses the current
font and color for rendering.
drawString(new String(bytes, off, len), x, y);
| public void | drawChars(char[] chars, int off, int len, int x, int y)Draws the text represented by character array. This method uses the
current font and color for rendering.
drawString(new String(chars, off, len), x, y);
| public abstract boolean | drawImage(java.awt.Image img, int x, int y, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Draws the specified image with the defined background color. The top left
corner of image will be drawn at point (x, y) in current coordinate
system. The image loading process notifies the specified Image Observer.
This method returns true if the image has loaded, otherwise it returns
false.
| public abstract boolean | drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)Draws the specified image. The top left corner of image will be drawn at
point (x, y) in current coordinate system. The image loading process
notifies the specified Image Observer. This method returns true if the
image has loaded, otherwise it returns false.
| public abstract boolean | drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Scales the specified image to fit in the specified rectangle and draws it
with the defined background color. The top left corner of the image will
be drawn at the point (x, y) in current coordinate system. The non-opaque
pixels will be drawn in the background color. The image loading process
notifies the specified Image Observer. This method returns true if the
image has loaded, otherwise it returns false.
| public abstract boolean | drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)Scales the specified image to fit in the specified rectangle and draws
it. The top left corner of the image will be drawn at the point (x, y) in
current coordinate system. The image loading process notifies the
specified Image Observer. This method returns true if the image has
loaded, otherwise it returns false.
| public abstract boolean | drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Scales the specified area of the specified image to fit in the rectangle
area defined by its corners coordinates and draws the sub-image with the
specified background color. The sub-image to be drawn is defined by its
top left corner coordinates (sx1, sy1) and bottom right corner
coordinates (sx2, sy2) computed with respect to the origin (top left
corner) of the source image. The non opaque pixels will be drawn in the
background color. The image loading process notifies specified Image
Observer. This method returns true if the image has loaded, otherwise it
returns false.
| public abstract boolean | drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.image.ImageObserver observer)Scales the specified area of the specified image to fit in the rectangle
area defined by its corners coordinates and draws the sub-image. The
sub-image to be drawn is defined by its top left corner coordinates (sx1,
sy1) and bottom right corner coordinates (sx2, sy2) computed with respect
to the origin (top left corner) of the source image. The image loading
process notifies specified Image Observer. This method returns true if
the image has loaded, otherwise it returns false.
| public abstract void | drawLine(int x1, int y1, int x2, int y2)Draws a line from the point (x1, y1) to the point (x2, y2). This method
draws the line with current color which can be changed by setColor(Color
c) method.
| public abstract void | drawOval(int x, int y, int width, int height)Draws the outline of an oval to fit in the rectangle defined by the given
width, height, and top left corner.
| public abstract void | drawPolygon(int[] xpoints, int[] ypoints, int npoints)Draws the outline of a polygon. The polygon vertices are defined by
points with xpoints[i], ypoints[i] as coordinates. The polygon edges are
the lines from the points with (xpoints[i-1], ypoints[i-1]) coordinates
to the points with (xpoints[i], ypoints[i]) coordinates, for 0 < i <
npoints +1.
| public void | drawPolygon(java.awt.Polygon p)Draws the outline of a polygon which is defined by Polygon object.
drawPolygon(p.xpoints, p.ypoints, p.npoints);
| public abstract void | drawPolyline(int[] xpoints, int[] ypoints, int npoints)Draws a set of connected lines which are defined by the x and y
coordinate arrays. The polyline is closed if coordinates of the first
point are the same as coordinates of the last point.
| public void | drawRect(int x, int y, int width, int height)Draws the rectangle with the specified width and length and top left
corner coordinates.
int[] xpoints = {
x, x, x + width, x + width
};
int[] ypoints = {
y, y + height, y + height, y
};
drawPolygon(xpoints, ypoints, 4);
| public abstract void | drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Draws the outline of a rectangle with round corners.
| public abstract void | drawString(java.text.AttributedCharacterIterator iterator, int x, int y)Draws a text defined by an iterator. The iterator should specify the font
for every character.
| public abstract void | drawString(java.lang.String str, int x, int y)Draws a text defined by a string. This method draws the text with current
font and color.
| public void | fill3DRect(int x, int y, int width, int height, boolean raised)Fills the highlighted outline of a rectangle.
// Note: lighter/darker colors should be used to draw 3d rect.
// The resulting rect is (width)x(height), same as fillRect.
// Stroke and paint attributes of the Graphics2D should be reset
// to the default values. fillRect is used instead of drawLine to
// bypass stroke reset/set and line rasterization.
Color color = getColor();
Color colorUp, colorDown;
if (raised) {
colorUp = color.brighter();
colorDown = color.darker();
setColor(color);
} else {
colorUp = color.darker();
colorDown = color.brighter();
setColor(colorUp);
}
width--;
height--;
fillRect(x + 1, y + 1, width - 1, height - 1);
setColor(colorUp);
fillRect(x, y, width, 1);
fillRect(x, y + 1, 1, height);
setColor(colorDown);
fillRect(x + width, y, 1, height);
fillRect(x + 1, y + height, width, 1);
| public abstract void | fillArc(int x, int y, int width, int height, int sa, int ea)Fills the arc covering the rectangle and using the current color. The
rectangle is defined by the origin point (X, Y) and dimensions (width and
height). The arc center is the the center of specified rectangle. The
angle origin is at the 3 o'clock position, and a positive angle gives
counter-clockwise rotation, a negative angle gives clockwise rotation.
| public abstract void | fillOval(int x, int y, int width, int height)Fills an oval with the current color where the oval is defined by the
bounding rectangle with the given width, height, and top left corner.
| public abstract void | fillPolygon(int[] xpoints, int[] ypoints, int npoints)Fills a polygon with the current color. The polygon vertices are defined
by the points with xpoints[i], ypoints[i] as coordinates. The polygon
edges are the lines from the points with (xpoints[i-1], ypoints[i-1])
coordinates to the points with (xpoints[i], ypoints[i]) coordinates, for
0 < i < npoints +1.
| public void | fillPolygon(java.awt.Polygon p)Fills the polygon with the current color.
fillPolygon(p.xpoints, p.ypoints, p.npoints);
| public abstract void | fillRect(int x, int y, int width, int height)Fills a rectangle with the current color. The rectangle is defined by its
width and length and top left corner coordinates.
| public abstract void | fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)Fills a round cornered rectangle with the current color.
| public void | finalize()Disposes of the Graphics.
| public abstract java.awt.Shape | getClip()Gets the clipping area.
| public java.awt.Rectangle | getClipBounds(java.awt.Rectangle r)Gets the bounds of the current clipping area as a rectangle and copies it
to an existing rectangle.
Shape clip = getClip();
if (clip != null) {
// TODO: Can we get shape bounds without creating Rectangle object?
Rectangle b = clip.getBounds();
r.x = b.x;
r.y = b.y;
r.width = b.width;
r.height = b.height;
}
return r;
| public abstract java.awt.Rectangle | getClipBounds()Gets the bounds of the current clipping area as a rectangle.
| public java.awt.Rectangle | getClipRect()Gets the bounds of the current clipping area as a rectangle.
return getClipBounds();
| public abstract java.awt.Color | getColor()Gets the current color of Graphics.
| public abstract java.awt.Font | getFont()Gets the current font of Graphics.
| public java.awt.FontMetrics | getFontMetrics()Gets the font metrics of the current font. The font metrics object
contains information about the rendering of a particular font.
return getFontMetrics(getFont());
| public abstract java.awt.FontMetrics | getFontMetrics(java.awt.Font font)Gets the font metrics of the specified font. The font metrics object
contains information about the rendering of a particular font.
| public boolean | hitClip(int x, int y, int width, int height)Determines whether or not the specified rectangle intersects the current
clipping area.
// TODO: Create package private method Rectangle.intersects(int, int,
// int, int);
return getClipBounds().intersects(new Rectangle(x, y, width, height));
| public abstract void | setClip(int x, int y, int width, int height)Sets the new clipping area specified by rectangle. The new clipping area
doesn't depend on the window's visibility. Rendering operations can't be
performed outside new clipping area.
| public abstract void | setClip(java.awt.Shape clip)Sets the new clipping area to be the area specified by Shape object. The
new clipping area doesn't depend on the window's visibility. Rendering
operations can't be performed outside new clipping area.
| public abstract void | setColor(java.awt.Color c)Sets the current Graphics color. All rendering operations with this
Graphics will use this color.
| public abstract void | setFont(java.awt.Font font)Sets the current Graphics font. All rendering operations with this
Graphics will use this font.
| public abstract void | setPaintMode()Sets the paint mode for the Graphics which overwrites all rendering
operations with the current color.
| public abstract void | setXORMode(java.awt.Color color)Sets the XOR mode for the Graphics which changes a pixel from the current
color to the specified XOR color.
| public java.lang.String | toString()Returns string which represents this Graphics object.
// TODO: Think about string representation of Graphics.
return "Graphics"; //$NON-NLS-1$
| public abstract void | translate(int x, int y)Translates the origin of Graphics current coordinate system to the point
with X, Y coordinates in the current coordinate system. All rendering
operation in this Graphics will be related to the new origin.
|
|