AWT 圖形類



簡介

Graphics 類是所有圖形上下文的抽象超類,允許應用程式在可以在各種裝置上實現的元件上或在螢幕外影像上繪製。

Graphics 物件封裝了 Java 支援的基本渲染操作所需的所有狀態資訊。狀態資訊包括以下屬性。

  • 要繪製的 Component 物件。

  • 用於渲染和裁剪座標的平移原點。

  • 當前剪輯區域。

  • 當前顏色。

  • 當前字型。

  • 當前邏輯畫素操作函式。

  • 當前 XOR 交替顏色

類宣告

以下是 java.awt.Graphics 類的宣告

public abstract class Graphics
   extends Object

類建構函式

序號建構函式 & 描述
1

Graphics() ()

構造一個新的 Graphics 物件。

類方法

序號方法 & 描述
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)

繪製指定矩形的 3D 高亮輪廓。

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)

使用當前顏色繪製 3D 高亮矩形。

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

Graphics 示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > AWT > com > tutorialspoint > gui >

AWTGraphicsDemo.java
package 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) {
      g.setColor(Color.GRAY);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 150);      
   }
}

使用命令提示符編譯程式。轉到 D:/ > AWT 並鍵入以下命令。

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

如果沒有任何錯誤,則表示編譯成功。使用以下命令執行程式。

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

驗證以下輸出

AWT Graphics
awt_graphics.htm
廣告

© . All rights reserved.