位置:首頁 > Java技術 > AWT > AWT Graphics2D類

AWT Graphics2D類

Graphics2D類擴展Graphics類的幾何坐標變換,色彩管理和文本布局提供更複雜的控製權。

類的聲明

以下是的聲明類java.awt.Graphics2D:

public abstract class Graphics2D
   extends Graphics

類的構造函數

S.N. 構造函數與說明
1 Graphics2D()
Constructs a new Graphics2D object.

類方法

S.N. 方法和說明
1 abstract void addRenderingHints(Map hints) 
Sets the values of an arbitrary number of preferences for the rendering algorithms.
2 abstract void clip(Shape s) 
Intersects the current Clip with the interior of the specified Shape and sets the Clip to the resulting intersection.
3 abstract void draw(Shape s) 
Strokes the outline of a Shape using the settings of the current Graphics2D context.
4 void draw3DRect(int x, int y, int width, int height, boolean raised) 
Draws a 3-D highlighted outline of the specified rectangle.
5 abstract void drawGlyphVector(GlyphVector g, float x, float y) 
Renders the text of the specified GlyphVector using the Graphics2D context's rendering attributes.
6 abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) 
Renders a BufferedImage that is filtered with a BufferedImageOp.
7 abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) 
Renders an image, applying a transform from image space into user space before drawing.
8 abstract void drawRenderableImage(RenderableImage img, AffineTransform xform) 
Renders a RenderableImage, applying a transform from image space into user space before drawing.
9 abstract void drawRenderedImage(RenderedImage img, AffineTransform xform) 
Renders a RenderedImage, applying a transform from image space into user space before drawing.
10 abstract void drawString(AttributedCharacterIterator iterator, float x, float y) 
Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.
11 abstract void drawString(AttributedCharacterIterator iterator, int x, int y) 
Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.
12 abstract void drawString(String str, float x, float y) 
Renders the text specified by the specified String, using the current text attribute state in the Graphics2D context.
13 abstract void drawString(String str, int x, int y) 
Renders the text of the specified String, using the current text attribute state in the Graphics2D context.
14 abstract void fill(Shape s) 
Fills the interior of a Shape using the settings of the Graphics2D context.
15 void fill3DRect(int x, int y, int width, int height, boolean raised) 
Paints a 3-D highlighted rectangle filled with the current color.
16 abstract Color getBackground() 
Returns the background color used for clearing a region.
17 abstract Composite getComposite() 
Returns the current Composite in the Graphics2D context.
18 abstract GraphicsConfiguration getDeviceConfiguration() 
Returns the device configuration associated with this Graphics2D.
19 abstract FontRenderContext getFontRenderContext() 
Get the rendering context of the Font within this Graphics2D context.
20 abstract Paint getPaint() 
Returns the current Paint of the Graphics2D context.
21 abstract Object getRenderingHint(RenderingHints.Key hintKey) 
Returns the value of a single preference for the rendering algorithms.
22 abstract RenderingHints getRenderingHints() 
Gets the preferences for the rendering algorithms.
23 abstract Stroke getStroke() 
Returns the current Stroke in the Graphics2D context.
24 abstract AffineTransform getTransform() 
Returns a copy of the current Transform in the Graphics2D context.
25 abstract boolean hit(Rectangle rect, Shape s, boolean onStroke) 
Checks whether or not the specified Shape intersects the specified Rectangle, which is in device space.
26 abstract void rotate(double theta)
Concatenates the current Graphics2D Transform with a rotation transform.
27 abstract void rotate(double theta, double x, double y) 
Concatenates the current Graphics2D Transform with a translated rotation transform.
28 abstract void scale(double sx, double sy) 
Concatenates the current Graphics2D Transform with a scaling transformation Subsequent rendering is resized according to the specified scaling factors relative to the previous scaling.
29 abstract void setBackground(Color color) 
Sets the background color for the Graphics2D context.
30 abstract void setComposite(Composite comp) 
Sets the Composite for the Graphics2D context.
31 abstract void setPaint(Paint paint) 
Sets the Paint attribute for the Graphics2D context.
32 abstract void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) 
Sets the value of a single preference for the rendering algorithms.
33 abstract void setRenderingHints(Map hints) 
Replaces the values of all preferences for the rendering algorithms with the specified hints.
34 abstract void setStroke(Stroke s) 
Sets the Stroke for the Graphics2D context.
35 abstract void setTransform(AffineTransform Tx) 
Overwrites the Transform in the Graphics2D context.
36 abstract void shear(double shx, double shy) 
Concatenates the current Graphics2D Transform with a shearing transform.
37 abstract void transform(AffineTransform Tx) 
Composes an AffineTransform object with the Transform in this Graphics2D according to the rule last-specified-first-applied.
38 abstract void translate(double tx, double ty) 
Concatenates the current Graphics2D Transform with a translation transform.
39 abstract void translate(int x, int y) 
Translates the origin of the Graphics2D context to the point (x, y) in the current coordinate system.

繼承的方法

這個類繼承的方法從以下類:

  • java.lang.Object

Graphics2D 實例

選擇使用任何編輯器創建以下java程序 D:/ > AWT > com > yiibai > gui >

AWTGraphicsDemo.java

package com.yiibai.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 comyiibaiguiAWTGraphicsDemo.java

如果冇有錯誤出現,這意味著編譯成功。使用下麵的命令來運行程序。

D:AWT>java com.yiibai.gui.AWTGraphicsDemo

驗證下麵的輸出

AWT Graphics2D