目录

Graphics

介绍 (Introduction)

Graphics类是所有图形上下文的抽象超类,它允许应用程序绘制到可以在各种设备上实现的组件,或者也可以在屏幕外图像上绘制。

Graphics对象封装了Java支持的基本呈现操作所需的所有状态信息。 状态信息包括以下属性。

  • 要绘制的Component对象。

  • 渲染和裁剪坐标的平移原点。

  • 当前剪辑。

  • 目前的颜色。

  • 当前的字体。

  • 当前逻辑像素操作功能。

  • 当前的XOR交替颜色

类声明

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

public abstract class Graphics
   extends Object

类构造函数

SN 构造函数和描述
1

Graphics() ()

构造一个新的Graphics对象。

类方法

SN 方法和描述
1

abstract void clearRect(int x, int y, int width, int height)

通过使用当前绘图表面的背景颜色填充指定的矩形来清除它。

2

abstract void clipRect(int x, int y, int width, int height)

将当前剪辑与指定的矩形相交。

3

abstract void copyArea(int x, int y, int width, int height, int dx, int dy)

将组件的区域复制dx和dy指定的距离。

4

abstract Graphics create()

创建一个新的Graphics对象,该对象是此Graphics对象的副本。

5

Graphics create(int x, int y, int width, int height)

基于此Graphics对象创建新的Graphics对象,但具有新的翻译和剪辑区域。

6

abstract void dispose()

处置此图形上下文并释放它正在使用的任何系统资源。

7

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

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

8

abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

绘制覆盖指定矩形的圆形或椭圆弧的轮廓。

9

void drawBytes(byte[] data, int offset, int length, int x, int y)

使用此图形上下文的当前字体和颜色绘制由指定字节数组给出的文本。

10

void drawChars(char[] data, int offset, int length, int x, int y)

使用此图形上下文的当前字体和颜色绘制指定字符数组给出的文本。

11

abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)

绘制尽可能多的指定图像。

12

abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)

绘制尽可能多的指定图像。

13

abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)

绘制已经缩放以适合指定矩形内部的指定图像。

14

abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)

绘制已经缩放以适合指定矩形内部的指定图像。

15

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)

绘制与当前可用的指定图像一样多的指定区域,即时缩放它以适合目标可绘制表面的指定区域。

16

abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

绘制与当前可用的指定图像一样多的指定区域,即时缩放它以适合目标可绘制表面的指定区域。

17

abstract void drawLine(int x1, int y1, int x2, int y2)

在此图形上下文的坐标系中,使用当前颜色在点(x1,y1)和(x2,y2)之间绘制一条线。

18

abstract void drawOval(int x, int y, int width, int height)

绘制椭圆的轮廓。

19

abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

绘制由x和y坐标数组定义的闭合多边形。

20

void drawPolygon(Polygon p)

绘制由指定的Polygon对象定义的多边形的轮廓。

21

abstract void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)

绘制由x和y坐标数组定义的一系列连通线。

22

void drawRect(int x, int y, int width, int height)

绘制指定矩形的轮廓。

23

abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

使用此图形上下文的当前颜色绘制轮廓圆角矩形。

24

abstract void drawString(AttributedCharacterIterator iterator, int x, int y)

根据TextAttribute类的规范,呈现指定迭代器的文本,并应用其属性。

25

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

使用此图形上下文的当前字体和颜色绘制由指定字符串给出的文本。

26

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

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

27

abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

填充覆盖指定矩形的圆形或椭圆形弧。

28

abstract void fillOval(int x, int y, int width, int height)

使用当前颜色填充由指定矩形限定的椭圆。

29

abstract void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)

填充由x和y坐标数组定义的闭合多边形。

30

void fillPolygon(Polygon p)

使用图形上下文的当前颜色填充由指定的Polygon对象定义的多边形。

31

abstract void fillRect(int x, int y, int width, int height)

填充指定的矩形。

32

abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

使用当前颜色填充指定的圆角矩形。

33

void finalize()

一旦不再引用此图形上下文,就处置它。

34

abstract Shape getClip()

获取当前剪切区域。

35

abstract Rectangle getClipBounds()

返回当前剪切区域的边界矩形。

36

Rectangle getClipBounds(Rectangle r)

返回当前剪切区域的边界矩形。

37

Rectangle getClipRect()

已过时。 从JDK 1.1版开始,由getClipBounds()取代。

38

abstract Color getColor()

获取此图形上下文的当前颜色。

39

abstract Font getFont()

获取当前字体。

40

FontMetrics getFontMetrics()

获取当前字体的字体度量。

41

abstract FontMetrics getFontMetrics(Font f)

获取指定字体的字体度量标准。

42

boolean hitClip(int x, int y, int width, int height)

如果指定的矩形区域可能与当前剪切区域相交,则返回true。

43

abstract void setClip(int x, int y, int width, int height)

将当前剪辑设置为给定坐标指定的矩形。

44

abstract void setClip(Shape clip)

将当前剪切区域设置为任意剪辑形状。

45

abstract void setColor(Color c)

将此图形上下文的当前颜色设置为指定的颜色。

46

abstract void setFont(Font font)

将此图形上下文的字体设置为指定的字体。

47

abstract void setPaintMode()

设置此图形上下文的绘制模式,以使用此图形上下文的当前颜色覆盖目标。

48

abstract void setXORMode(Color c1)

设置此图形上下文的绘制模式,以在此图形上下文的当前颜色和新指定颜色之间切换。

49

String toString()

返回表示此Graphics对象值的String对象。

50

abstract void translate(int x, int y)

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

方法继承

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

  • java.lang.Object

图形示例

使用您选择的任何编辑器创建以下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) {
      g.setColor(Color.GRAY);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g.setFont(font);
      g.drawString("Welcome to IoWiki", 50, 150);      
   }
}

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

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

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

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

验证以下输出

AWT图形
↑回到顶部↑
WIKI教程 @2018