AWT Graphics2D 類



介紹

Graphics2D 類擴充套件了 Graphics 類,提供了對幾何圖形、座標變換、顏色管理和文字佈局的更精細的控制。

類宣告

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

public abstract class Graphics2D
   extends Graphics

類建構函式

序號建構函式 & 描述
1

Graphics2D()

構造一個新的 Graphics2D 物件。

類方法

序號方法 & 描述
1

abstract void addRenderingHints(Map<?,?> hints)

設定渲染演算法的任意數量的首選項的值。

2

abstract void clip(Shape s)

將當前裁剪區域與指定 Shape 的內部相交,並將裁剪區域設定為生成的交集。

3

abstract void draw(Shape s)

使用當前 Graphics2D 上下文的設定描繪 Shape 的輪廓。

4

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

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

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)

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

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 shear(double shx, double shy)

將當前 Graphics2D Transform 與剪下變換連線。

37

abstract void transform(AffineTransform Tx)

根據“最後指定,首先應用”的規則,將 AffineTransform 物件與此 Graphics2D 中的 Transform 組合。

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 > 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) {
      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\tutorialspoint\gui\AWTGraphicsDemo.java

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

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

驗證以下輸出

AWT Graphics2D
awt_graphics.htm
廣告
© . All rights reserved.