java.lang.Objectjava.awt.Graphics
public abstract class Graphics
Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制。
Graphics 对象封装了 Java 支持的基本呈现操作所需的状态信息。此状态信息包括以下属性:
Component 对象。 setXORMode(java.awt.Color))。 坐标是无限细分的,并且位于输出设备的像素之间。绘制图形轮廓的操作是通过使用像素大小的画笔遍历像素间无限细分路径的操作,画笔从路径上的锚点向下和向右绘制。填充图形的操作是填充图形内部区域无限细分路径操作。呈现水平文本的操作是呈现字符字形完全位于基线坐标之上的上升部分。
图形画笔从要遍历的路径向下和向右绘制。其含义如下:
所有作为此 Graphics 对象方法的参数而出现的坐标,都是相对于调用该方法前的此 Graphics 对象转换原点的。
所有呈现操作仅修改当前剪贴区所限定区域内的像素,此剪贴区是由用户空间中的 Shape 指定的,并通过使用 Graphics 对象的程序来控制。此用户剪贴区 被转换到设备空间中,并与设备剪贴区 组合,后者是通过窗口可见性和设备范围定义的。用户剪贴区和设备剪贴区的组合定义复合剪贴区,复合剪贴区确定最终的剪贴区域。用户剪贴区不能由呈现系统修改,以反映得到的复合剪贴区。用户剪贴区只能通过 setClip 或 clipRect 方法更改。所有的绘制或写入都以当前的颜色、当前绘图模式和当前字体完成。
Component,
clipRect(int, int, int, int),
setColor(java.awt.Color),
setPaintMode(),
setXORMode(java.awt.Color),
setFont(java.awt.Font)
| 构造方法摘要 | |
|---|---|
protected |
Graphics() 构造一个新的 Graphics 对象。 |
| 方法摘要 | |
|---|---|
abstract void |
clearRect(int x, int y, int width, int height) 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。 |
abstract void |
clipRect(int x, int y, int width, int height) 将当前剪贴区与指定的矩形相交。 |
abstract void |
copyArea(int x, int y, int width, int height, int dx, int dy) 将组件的区域复制到由 dx 和 dy 指定的距离处。 |
abstract Graphics |
create() 创建一个新的 Graphics 对象,它是此 Graphics 对象的副本。 |
Graphics |
create(int x, int y, int width, int height) 基于此 Graphics 对象创建一个新的 Graphics 对象,但是使用新的转换和剪贴区域。 |
abstract void |
dispose() 释放此图形的上下文以及它使用的所有系统资源。 |
void |
draw3DRect(int x, int y, int width, int height, boolean raised) 绘制指定矩形的 3-D 高亮显示边框。 |
abstract void |
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) 绘制一个覆盖指定矩形的圆弧或椭圆弧边框。 |
void |
drawBytes(byte[] data, int offset, int length, int x, int y) 使用此图形上下文的当前字体和颜色绘制由指定 byte 数组给定的文本。 |
void |
drawChars(char[] data, int offset, int length, int x, int y) 使用此图形上下文的当前字体和颜色绘制由指定字符数组给定的文本。 |
abstract boolean |
drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) 绘制指定图像中当前可用的图像。 |
abstract boolean |
drawImage(Image img, int x, int y, ImageObserver observer) 绘制指定图像中当前可用的图像。 |
abstract boolean |
drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) 绘制指定图像中已缩放到适合指定矩形内部的图像。 |
abstract boolean |
drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) 绘制指定图像中已缩放到适合指定矩形内部的图像。 |
abstract boolean |
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) 绘制当前可用的指定图像的指定区域,动态地缩放图像使其符合目标绘制表面的指定区域。 |
abstract boolean |
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) 绘制当前可用的指定图像的指定区域,动态地缩放图像使其符合目标绘制表面的指定区域。 |
abstract void |
drawLine(int x1, int y1, int x2, int y2) 在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。 |
abstract void |
drawOval(int x, int y, int width, int height) 绘制椭圆的边框。 |
abstract void |
drawPolygon(int[] xPoints, int[] yPoints, int nPoints) 绘制一个由 x 和 y 坐标数组定义的闭合多边形。 |
void |
drawPolygon(Polygon p) 绘制由指定的 Polygon 对象定义的多边形边框。 |
abstract void |
drawPolyline(int[] xPoints, int[] yPoints, int nPoints) 绘制由 x 和 y 坐标数组定义的一系列连接线。 |
void |
drawRect(int x, int y, int width, int height) 绘制指定矩形的边框。 |
abstract void |
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) 用此图形上下文的当前颜色绘制圆角矩形的边框。 |
abstract void |
drawString(AttributedCharacterIterator iterator, int x, int y) 依据 TextAttribute 类的规范应用指定迭代器的属性,呈现迭代器的文本。 |
abstract void |
drawString(String str, int x, int y) 使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。 |
void |
fill3DRect(int x, int y, int width, int height, boolean raised) 绘制一个用当前颜色填充的 3-D 高亮显示矩形。 |
abstract void |
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 填充覆盖指定矩形的圆弧或椭圆弧。 |
abstract void |
fillOval(int x, int y, int width, int height) 使用当前颜色填充外接指定矩形框的椭圆。 |
abstract void |
fillPolygon(int[] xPoints, int[] yPoints, int nPoints) 填充由 x 和 y 坐标数组定义的闭合多边形。 |
void |
fillPolygon(Polygon p) 用图形上下文的当前颜色填充指定 Polygon 对象定义的多边形。 |
abstract void |
fillRect(int x, int y, int width, int height) 填充指定的矩形。 |
abstract void |
fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) 用当前颜色填充指定的圆角矩形。 |
void |
finalize() 一旦不再引用此图形上下文就释放它。 |
abstract Shape |
getClip() 获取当前剪贴区域。 |
abstract Rectangle |
getClipBounds() 返回当前剪贴区域的边界矩形。 |
Rectangle |
getClipBounds(Rectangle r) 返回当前剪贴区域的边界矩形。 |
Rectangle |
getClipRect() 已过时。 从 JDK version 1.1 开始,由 getClipBounds() 取代。 |
abstract Color |
getColor() 获取此图形上下文的当前颜色。 |
abstract Font |
getFont() 获取当前字体。 |
FontMetrics |
getFontMetrics() 获取当前字体的字体规格。 |
abstract FontMetrics |
getFontMetrics(Font f) 获取指定字体的字体规格。 |
boolean |
hitClip(int x, int y, int width, int height) 如果指定的矩形区域与当前的剪贴区域相交,则返回 true。 |
abstract void |
setClip(int x, int y, int width, int height) 将当前剪贴区设置为由给定坐标指定的矩形。 |
abstract void |
setClip(Shape clip) 将当前剪贴区域设置为任意的剪贴形状。 |
abstract void |
setColor(Color c) 将此图形上下文的当前颜色设置为指定颜色。 |
abstract void |
setFont(Font font) 将此图形上下文的字体设置为指定字体。 |
abstract void |
setPaintMode() 设置将此图形上下文的绘图模式,以便通过此图形上下文中的当前颜色来覆盖目标。 |
abstract void |
setXORMode(Color c1) 将此图形上下文的绘图模式设置为在此图形上下文的当前颜色和新的指定颜色之间交替。 |
String |
toString() 返回表示此 Graphics 对象值的 String 对象。 |
abstract void |
translate(int x, int y) 将图形上下文的原点平移到当前坐标系中的点 (x, y)。 |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 构造方法详细信息 |
|---|
protected Graphics()
Graphics 对象。此构造方法是用于图形上下文的默认构造方法。
因为 Graphics 是一个抽象类,所以应用程序不能直接调用此构造方法。图形上下文从其他图形上下文获取,或者通过在组件上调用 getGraphics 来创建。
create(),
Component.getGraphics()
| 方法详细信息 |
|---|
public abstract Graphics create()
Graphics 对象,它是此
Graphics 对象的副本。
public Graphics create(int x, int y, int width, int height)
Graphics 对象创建一个新的
Graphics 对象,但是使用新的转换和剪贴区域。新
Graphics 对象的原点平移到指定点 (
x,
y)。其剪贴区域由原始剪贴区域和指定矩形的交集确定。各个参数均在原始
Graphics 对象的坐标系内进行解释。新图形上下文除以下两方面之外,其他与原始图形上下文相同:
0, 0) 与原图形上下文中的点 (x, y) 相同。 0, 0),其大小由 width 和 height 参数指定。
x -
x 坐标。
y -
y 坐标。
width - 剪贴矩形的宽度。
height - 剪贴矩形的高度。
translate(int, int),
clipRect(int, int, int, int)
public abstract void translate(int x,
int y)
x -
x 坐标。
y -
y 坐标。
public abstract Color getColor()
Color,
setColor(Color)
public abstract void setColor(Color c)
c - 新的呈现颜色。
Color,
getColor()
public abstract void setPaintMode()
public abstract void setXORMode(Color c1)
当执行绘制操作时,当前颜色的像素更改为指定的颜色,反之亦然。
这两种颜色以外的像素以不可预测但可逆的方式更改;如果相同的图形被绘制两次,则所有像素恢复到原始值。
c1 - XOR 交替颜色
public abstract Font getFont()
Font,
setFont(Font)
public abstract void setFont(Font font)
public FontMetrics getFontMetrics()
getFont(),
FontMetrics,
getFontMetrics(Font)
public abstract FontMetrics getFontMetrics(Font f)
f - 指定的字体
getFont(),
FontMetrics,
getFontMetrics()
public abstract Rectangle getClipBounds()
setClip(null) 清除了剪贴区,则此方法返回
null。该矩形内的坐标相对于此图形上下文的坐标系原点。
null。
getClip(),
clipRect(int, int, int, int),
setClip(int, int, int, int),
setClip(Shape)
public abstract void clipRect(int x,
int y,
int width,
int height)
setClip(null) 清除了剪贴区而导致没有当前剪贴区域,那么指定的矩形将成为新的剪贴区。此方法设置用户剪贴区,用户剪贴区与设备边界和窗口可见性所关联的剪贴区无关。此方法只能用来缩小当前剪贴区。要扩大当前剪贴区,请使用 setClip 方法。呈现操作对剪贴区域的外部没有影响。
x - 要与剪贴区相交的矩形的 x 坐标
y - 要与剪贴区相交的矩形的 y 坐标
width - 要与剪贴区相交的矩形的宽度
height - 要与剪贴区相交的矩形的高度
setClip(int, int, int, int),
setClip(Shape)
public abstract void setClip(int x,
int y,
int width,
int height)
x - 新剪贴区矩形的
x 坐标。
y - 新剪贴区矩形的
y 坐标。
width - 新剪贴区矩形的宽度。
height - 新剪贴区矩形的高度。
clipRect(int, int, int, int),
setClip(Shape),
getClip()
public abstract Shape getClip()
setClip(null) 清除了剪贴区,那么此方法返回
null。
Shape 对象;如果未设置剪贴区,则返回
null。
getClipBounds(),
clipRect(int, int, int, int),
setClip(int, int, int, int),
setClip(Shape)
public abstract void setClip(Shape clip)
Shape 接口的对象都能用于设置剪贴区。只保证支持通过
getClip 方法和
Rectangle 对象获取的
Shape 对象。此方法设置用户剪贴区,用户剪贴区与设备边界和窗口可见性关联的剪贴区无关。
clip - 用来设置剪贴形状的
Shape
getClip(),
clipRect(int, int, int, int),
setClip(int, int, int, int)
public abstract void copyArea(int x,
int y,
int width,
int height,
int dx,
int dy)
dx 和
dy 指定的距离处。此方法从
x 和
y 指定的点向下和向右复制。要将组件的区域向左或向上复制,请指定负的
dx 或
dy 值。如果源矩形的一部分位于组件边界的外部,或者被另一个窗口或组件遮掩,那么
copyArea 将无法复制相关的像素。通过调用组件的
paint 方法可刷新被忽略的区域。
x - 源矩形的
x 坐标。
y - 源矩形的
y 坐标。
width - 源矩形的宽度。
height - 源矩形的高度。
dx - 复制像素的水平距离。
dy - 复制像素的垂直距离。
public abstract void drawLine(int x1,
int y1,
int x2,
int y2)
(x1, y1) 和
(x2, y2) 之间画一条线。
x1 - 第一个点的
x 坐标。
y1 - 第一个点的
y 坐标。
x2 - 第二个点的
x 坐标。
y2 - 第二个点的
y 坐标。
public abstract void fillRect(int x,
int y,
int width,
int height)
x 和
x + width - 1。上边缘和下边缘分别位于
y 和
y + height - 1。得到的矩形覆盖
width 像素宽乘以
height 像素高的区域。使用图形上下文的当前颜色填充该矩形。
x - 要填充矩形的
x 坐标。
y - 要填充矩形的
y 坐标。
width - 要填充矩形的宽度。
height - 要填充矩形的高度。
clearRect(int, int, int, int),
drawRect(int, int, int, int)
public void drawRect(int x,
int y,
int width,
int height)
x 和
x + width。上边缘和下边缘分别位于
y 和
y + height。使用图形上下文的当前颜色绘制该矩形。
x - 要绘制矩形的
x 坐标。
y - 要绘制矩形的
y 坐标。
width - 要绘制矩形的宽度。
height - 要绘制矩形的高度。
fillRect(int, int, int, int),
clearRect(int, int, int, int)
public abstract void clearRect(int x,
int y,
int width,
int height)
从 Java 1.1 开始,离屏图像的背景色可能与系统相关。应用程序应该使用 setColor,然后使用 fillRect,以确保离屏图像被清除为指定颜色。
x - 要清除矩形的
x 坐标。
y - 要清除矩形的
y 坐标。
width - 要清除矩形的宽度。
height - 要清除矩形的高度。
fillRect(int, int, int, int),
drawRect(int, int, int, int),
setColor(java.awt.Color),
setPaintMode(),
setXORMode(java.awt.Color)
public abstract void drawRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight)
x 和
x + width。矩形的上边缘和下边缘分别位于
y 和
y + height。
x - 要绘制矩形的
x 坐标。
y - 要绘制矩形的
y 坐标。
width - 要绘制矩形的宽度。
height - 要绘制矩形的高度。
arcWidth - 4 个角弧度的水平直径。
arcHeight - 4 个角弧度的垂直直径。
fillRoundRect(int, int, int, int, int, int)
public abstract void fillRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight)
x 和
x + width - 1。矩形的上边缘和下边缘分别位于
y 和
y + height - 1。
x - 要填充矩形的
x 坐标。
y - 要填充矩形的
y 坐标。
width - 要填充矩形的宽度。
height - 要填充矩形的高度。
arcWidth - 4 个角弧度的水平直径。
arcHeight - 4 个角弧度的垂直直径。
drawRoundRect(int, int, int, int, int, int)
public void draw3DRect(int x,
int y,
int width,
int height,
boolean raised)
高亮显示效果所用的颜色根据当前颜色确定。得到的矩形覆盖 width + 1 像素宽乘以 height + 1 像素高的区域。
x - 要绘制矩形的
x 坐标。
y - 要绘制矩形的
y 坐标。
width - 要绘制矩形的宽度。
height - 要绘制矩形的高度。
raised - 一个用于确定矩形是凸出平面显示还是凹入平面显示的 boolean 值。
fill3DRect(int, int, int, int, boolean)
public void fill3DRect(int x,
int y,
int width,
int height,
boolean raised)
x - 要填充矩形的
x 坐标。
y - 要填充矩形的
y 坐标。
width - 要填充矩形的宽度。
height - 要填充矩形的高度。
raised - 一个用于确定矩形是凸出平面显示还是凹入平面显示的 boolean 值。
draw3DRect(int, int, int, int, boolean)
public abstract void drawOval(int x,
int y,
int width,
int height)
x、
y、
width 和
height 参数指定的矩形中。
椭圆覆盖区域的宽度为 width + 1 像素,高度为 height + 1 像素。