AWT Graphics2D Class



Introduction

The Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout.

Class declaration

Following is the declaration for java.awt.Graphics2D class:

public abstract class Graphics2D
   extends Graphics

Class constructors

S.N.Constructor & Description
1

Graphics2D()

Constructs a new Graphics2D object.

Class methods

S.N.Method & Description
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 rulelast-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.

Methods inherited

This class inherits methods from the following classes:

  • java.lang.Object

Graphics2D Example

Create the following java program using any editor of your choice in say 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); 
   }
}

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

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

If no error comes that means compilation is successful. Run the program using following command.

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

Verify the following output

AWT Graphics2D
awt_graphics.htm
Advertisements