Graphics
是什么?
Graphics
是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制
Graphics
对象封装了 Java 支持的基本呈现操作所需的状态信息。此状态信息包括以下属性:
- 要在其上绘制的
Component
对象。 - 呈现和剪贴坐标的转换原点。
- 当前剪贴区。
- 当前颜色。
- 当前字体。
- 当前逻辑像素操作函数(XOR 或 Paint)。
- 当前 XOR 交替颜色
Graphics2D是什么?
Graphics2D
类是Graphics
的子类,以提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制。
操作类型
应用方法
abstract void addRenderingHints(Map<?,?> hints)
为呈现算法设置任意数量的首选项值。
abstract void clip(Shape s)
将当前 Clip 与指定 Shape 的内部区域相交,并将 Clip 设置为所得的交集。
abstract void draw(Shape s)
使用当前 Graphics2D 上下文的设置勾画 Shape 的轮廓。
void draw3DRect(int x, int y, int width, int height, boolean raised)
绘制指定矩形的 3-D 高亮显示边框。
abstract void drawGlyphVector(GlyphVector g, float x, float y)
使用 Graphics2D 上下文的呈现属性,呈现指定 GlyphVector 的文本。
abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
呈现使用 BufferedImageOp 过滤的 BufferedImage。
abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
呈现一个图像,在绘制前进行从图像空间到用户空间的转换。
abstract void drawRenderableImage(RenderableImage img, AffineTransform xform)
呈现 RenderableImage,在绘制前进行从图像空间到用户空间的转换。
abstract void drawRenderedImage(RenderedImage img, AffineTransform xform)
呈现 RenderedImage,在绘制前进行从图像空间到用户空间的转换。
abstract void drawString(AttributedCharacterIterator iterator, float x, float y)
依照 TextAttribute 类的规范应用指定迭代器的属性,呈现指定迭代器的文本。
abstract void drawString(AttributedCharacterIterator iterator, int x, int y)
依照 TextAttribute 类的规范应用指定迭代器的属性,呈现指定迭代器的文本。
abstract void drawString(String str, float x, float y)
使用 Graphics2D 上下文中当前文本属性状态呈现由指定 String 指定的文本。
abstract void drawString(String str, int x, int y)
使用 Graphics2D 上下文中的当前文本属性状态呈现指定的 String 的文本。
abstract void fill(Shape s)
使用 Graphics2D 上下文的设置,填充 Shape 的内部区域。
void fill3DRect(int x, int y, int width, int height, boolean raised)
绘制一个用当前颜色填充的 3-D 高亮显示矩形。
abstract Color getBackground()
返回用于清除区域的背景色。
abstract Composite getComposite()
返回 Graphics2D 上下文中的当前 Composite
abstract GraphicsConfiguration getDeviceConfiguration()
返回与此 Graphics2D 关联的设备配置。
abstract FontRenderContext getFontRenderContext()
获取此 Graphics2D 上下文中 Font 的呈现上下文。
abstract Paint getPaint()
返回 Graphics2D 上下文中的当前 Paint
abstract Object getRenderingHint(RenderingHints.Key hintKey)
返回呈现算法的单个首选项的值。
abstract RenderingHints getRenderingHints()
获取呈现算法的首选项。
abstract Stroke getStroke()
返回 Graphics2D 上下文中的当前 Stroke
abstract AffineTransform getTransform()
返回 Graphics2D 上下文中当前 Transform 的副本。
abstract boolean hit(Rectangle rect, Shape s, boolean onStroke)
检查指定的 Shape 是否与设备空间中的指定 Rectangle 相交。
abstract void rotate(double theta)
将当前的 Graphics2D Transform 与旋转转换连接。
abstract void rotate(double theta, double x, double y)
将当前的 Graphics2D Transform 与平移后的旋转转换连接。
abstract void scale(double sx, double sy)
将当前 Graphics2D Transform 与缩放转换连接。
abstract void setBackground(Color color)
设置 Graphics2D 上下文的背景色。
abstract void setComposite(Composite comp)
为 Graphics2D 上下文设置 Composite。
abstract void setPaint(Paint paint)
为 Graphics2D 上下文设置 Paint 属性。
abstract void setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
为呈现算法设置单个首选项的值。
abstract void setRenderingHints(Map<?,?> hints)
使用指定的 hints 替换用于所有呈现算法首选项的值。
abstract void setStroke(Stroke s)
为 Graphics2D 上下文设置 Stroke
abstract void setTransform(AffineTransform Tx)
重写 Graphics2D 上下文中的 Transform。
abstract void shear(double shx, double shy)
将当前 Graphics2D Transform 与剪裁转换连接。
abstract void transform(AffineTransform Tx)
根据“最后指定首先应用”规则,使用此 Graphics2D 中的 Transform 组合 AffineTransform 对象。
abstract void translate(double tx, double ty)
将当前 Graphics2D Transform 与平移转换连接。
abstract void translate(int x, int y)
将 Graphics2D 上下文的原点平移到当前坐标系中的点 (x, y)。