目录

Graphics2D

介绍 (Introduction)

Graphics2D类扩展了Graphics类,以提供对几何,坐标转换,颜色管理和文本布局的更复杂控制。

类声明

以下是java.awt.Graphics2D类的声明:

public abstract class Graphics2D
   extends Graphics

类构造函数

SN 构造函数和描述
1

Graphics2D()

构造一个新的Graphics2D对象。

类方法

SN 方法和描述
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)

绘制指定矩形的三维突出显示轮廓。

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过滤的BufferedImage。

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上下文中的当前文本属性状态呈现指定String指定的文本

13

abstract void drawString(String str, int x, int y)

使用Graphics2D上下文中的当前文本属性状态呈现指定String的文本。

14

abstract void fill(Shape s)

使用Graphics2D上下文的设置填充Shape的内部。

15

void fill3DRect(int x, int y, int width, int height, boolean raised)

绘制一个用当前颜色填充的三维高亮矩形。

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变换与缩放变换连接后续渲染根据相对于先前缩放的指定缩放因子来调整大小。

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上下文的描边。

35

abstract void setTransform(AffineTransform Tx)

覆盖Graphics2D上下文中的Transform。

36

abstract void shear(double shx, double shy)

使用剪切变换连接当前的Graphics2D Transform。

37

abstract void transform(AffineTransform Tx)

根据rulelast-specified-first-applied,使用此Graphics2D中的Transform组合AffineTransform对象。

38

abstract void translate(double tx, double ty)

将当前的Graphics2D Transform与转换变换连接起来。

39

abstract void translate(int x, int y)

将Graphics2D上下文的原点转换为当前坐标系中的点(x,y)。

方法继承

该类继承以下类中的方法:

  • java.lang.Object

Graphics2D示例

使用您选择的任何编辑器创建以下java程序,例如D:/ 》 AWT 》 com 》 iowiki 》 gui 》

AWTGraphicsDemo.java
package com.iowiki.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 IoWiki", 50, 70); 
   }
}

使用命令提示符编译程序。 转到D:/ 》 AWT并键入以下命令。

D:\AWT>javac com\iowiki\gui\AWTGraphicsDemo.java

如果没有错误,那意味着编译成功。 使用以下命令运行程序。

D:\AWT>java com.iowiki.gui.AWTGraphicsDemo

验证以下输出

AWT Graphics2D
↑回到顶部↑
WIKI教程 @2018