AWT Graphics2D 类
简介
Graphics2D 类扩展了 Graphics 类,以提供对几何图形、坐标变换、颜色管理和文本布局的更复杂的控制。
类声明
以下是 java.awt.Graphics2D 类的声明:
public abstract class Graphics2D extends Graphics
类构造函数
S.N. | 构造函数 &描述 |
---|---|
1 | Graphics2D() 构造一个新的 Graphics2D 对象。 |
类方法
S.N. | 方法 &描述 |
---|---|
1 | abstract void addRenderingHints(Map<?,?> hints) 为渲染算法设置任意数量的首选项的值。 |
2 | abstract void clip(Shape s) 将当前 Clip 与指定 Shape 的内部相交,并将 Clip 设置为结果交集。 |
3 | abstract void draw(Shape s) 使用当前 Graphics2D 上下文的设置描边 Shape 的轮廓。 |
4 | void draw3DRect(int x, int y, int width, int height, boolean raised) 绘制指定矩形的 3-D 高亮轮廓。 |
5 | abstract void drawGlyphVector(GlyphVector g, float x, float y) 使用 Graphics2D 上下文的渲染属性渲染指定 GlyphVector 的文本。 |
6 | abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) 渲染使用BufferedImageOp。 |
7 | abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) 渲染图像,在绘制之前将图像空间的变换应用到用户空间。 |
8 | abstract void drawRenderableImage(RenderableImage img, AffineTransform xform) 渲染 RenderableImage,在绘制之前将图像空间的变换应用到用户空间。 |
9 | abstract void drawRenderedImage(RenderedImage img, AffineTransform xform) 渲染 RenderedImage,在绘制之前将图像空间的变换应用到用户空间绘制。 |
10 | abstract void drawString(AttributedCharacterIterator iterator, float x, float y) 根据 TextAttribute 类的规范,应用其属性渲染指定迭代器的文本。 |
11 | abstract void drawString(AttributedCharacterIterator iterator, int x, int y) 根据 TextAttribute 类的规范,应用其属性渲染指定迭代器的文本。 |
12 | abstract void drawString(String str, float x, float y) 根据指定字符串,使用 Graphics2D 上下文中的当前文本属性状态 |
13 | abstract void drawString(String str, int x, int y) 使用 Graphics2D 上下文中的当前文本属性状态,呈现指定字符串的文本。 |
14 | abstract void fill(Shape s) 使用 Graphics2D 上下文的设置填充 Shape 的内部。 |
15 | void fill3DRect(int x, int y, int width, int height, boolean raised) 绘制一个用当前颜色填充的 3-D 高亮矩形。 |
16 | abstract Color getBackground() 返回用于清除区域的背景颜色。 |
17 | abstract Composite getComposite() 返回 Graphics2D 上下文中的当前 Composite。 |
18 | abstract GraphicsConfiguration getDeviceConfiguration() 返回与此 Graphics2D 关联的设备配置。 |
19 | abstract FontRenderContext getFontRenderContext() 获取此 Graphics2D 上下文中的 Font 的渲染上下文。 |
20 | abstract Paint getPaint() 返回 Graphics2D 上下文的当前 Paint。 |
21 | abstract Object getRenderingHint(RenderingHints.Key hintKey) 返回渲染算法的单个首选项的值。 |
22 | abstract RenderingHints getRenderingHints() 获取渲染的首选项算法。 |
23 | abstract Stroke getStroke() 返回 Graphics2D 上下文中的当前 Stroke。 |
24 | abstract AffineTransform getTransform() 返回 Graphics2D 上下文中的当前 Transform 的副本。 |
25 | abstract boolean hit(Rectangle rect, Shape s, boolean onStroke) 检查指定的 Shape 是否与设备中的指定 Rectangle 相交空间。 |
26 | abstract void rotate(double theta) 将当前的 Graphics2D Transform 与旋转变换连接起来。 |
27 | abstract void rotate(double theta, double x, double y) 将当前的 Graphics2D Transform 与平移的旋转变换连接起来。 |
28 | abstract void scale(double sx, double sy) 将当前的 Graphics2D Transform 与缩放变换连接起来,后续渲染将根据相对于前一个渲染的指定缩放因子调整大小缩放。 |
29 | abstract void setBackground(Color color) 设置 Graphics2D 上下文的背景颜色。 |
30 | abstract void setComposite(Composite comp) 设置 Graphics2D 上下文的 Composite。 |
31 | abstract void setPaint(Paint paint) 设置 Graphics2D 上下文的 Paint 属性。 |
32 | abstract void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) 设置渲染算法的单个首选项的值。 |
33 | abstract void setRenderingHints(Map<?,?> hints) 用指定的提示替换渲染算法的所有首选项的值。 |
34 | abstract void setStroke(Stroke s) 设置 Graphics2D 上下文的 Stroke。 |
35 | abstract void setTransform(AffineTransform Tx) 覆盖 Graphics2D 上下文中的 Transform。 |
36 | abstract void sheath(double shx, double shy) 将当前 Graphics2D Transform 与剪切变换连接起来。 |
37 | abstract void transform(AffineTransform Tx) 根据规则last-specified-first-applied,用此 Graphics2D 中的 Transform 组合一个 AffineTransform 对象。 |
38 | abstract void Translation(double tx, double ty) 将当前 Graphics2D Transform 与平移变换连接起来。 |
39 | abstract void Translation(int x, int y) 将 Graphics2D 上下文的原点平移到当前坐标系中的点 (x, y)。 |
继承的方法
此类从以下类继承方法:
java.lang.Object
Graphics2D 示例
使用您选择的任何编辑器在 D:/ > AWT > com > tutorialspoint > gui > 中创建以下 Java 程序
AWTGraphicsDemo.javapackage com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class AWTGraphicsDemo extends Frame { public AWTGraphicsDemo(){ super("Java AWT Examples"); prepareGUI(); } public static void main(String[] args){ AWTGraphicsDemo awtGraphicsDemo = new AWTGraphicsDemo(); awtGraphicsDemo.setVisible(true); } private void prepareGUI(){ setSize(400,400); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); } @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Serif", Font.PLAIN, 24); g2.setFont(font); g2.drawString("Welcome to TutorialsPoint", 50, 70); } }
使用命令提示符编译程序。转到 D:/ > AWT 并键入以下命令。
D:\AWT>javac com utorialspoint\gui\AWTGraphicsDemo.java
如果没有错误,则表示编译成功。使用以下命令运行程序。
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
验证以下输出
