AWT Graphics2D Class

The Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout.
Following is the declaration for java.awt.Graphics2D class:
public abstract class Graphics2D
   extends Graphics
Graphics2D()
 Constructs a new Graphics2D object.
abstract void addRenderingHints(Map<?,?> hints) 
Sets the values of an arbitrary number of preferences for the rendering algorithms.
abstract void clip(Shape s) 
Intersects the current Clip with the interior of the specified Shape and sets the Clip to the resulting intersection.
abstract void draw(Shape s) 
Strokes the outline of a Shape using the settings of the current Graphics2D context.
void draw3DRect(int x, int y, int width, int height, boolean raised) 
Draws a 3-D highlighted outline of the specified rectangle.
abstract void drawGlyphVector(GlyphVector g, float x, float y)
Renders the text of the specified GlyphVector using the Graphics2D context's rendering attributes.
abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) 
Renders a BufferedImage that is filtered with a BufferedImageOp.
abstract  boolean	drawImage(Image img, AffineTransform xform, ImageObserver obs) 
Renders an image, applying a transform from image space into user space before drawing.
abstract void drawRenderableImage(RenderableImage img, AffineTransform xform) 
Renders a RenderableImage, applying a transform from image space into user space before drawing.
abstract void drawRenderedImage(RenderedImage img, AffineTransform xform) 
Renders a RenderedImage, applying a transform from image space into user space before drawing.
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.
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.
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
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.
abstract void fill(Shape s) 
Fills the interior of a Shape using the settings of the Graphics2D context.
void fill3DRect(int x, int y, int width, int height, boolean raised) 
Paints a 3-D highlighted rectangle filled with the current color.
abstract Color getBackground() 
Returns the background color used for clearing a region.
abstract Composite getComposite() 
Returns the current Composite in the Graphics2D context.
abstract GraphicsConfiguration getDeviceConfiguration()
Returns the device configuration associated with this Graphics2D.
abstract FontRenderContext getFontRenderContext() 
Get the rendering context of the Font within this Graphics2D context.
abstract Paint	getPaint() 
Returns the current Paint of the Graphics2D context.
abstract Object getRenderingHint(RenderingHints.Key hintKey)
Returns the value of a single preference for the rendering algorithms.
abstract RenderingHints getRenderingHints() 
Gets the preferences for the rendering algorithms.
abstract Stroke getStroke() 
Returns the current Stroke in the Graphics2D context.
abstract AffineTransform getTransform() 
Returns a copy of the current Transform in the Graphics2D context.
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.
abstract void rotate(double theta)
Concatenates the current Graphics2D Transform with a rotation transform.
abstract void rotate(double theta, double x, double y) 
Concatenates the current Graphics2D Transform with a translated rotation transform.
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.
abstract void setBackground(Color color) 
Sets the background color for the Graphics2D context.
abstract void setComposite(Composite comp) 
Sets the Composite for the Graphics2D context.
abstract void setPaint(Paint paint) 
Sets the Paint attribute for the Graphics2D context.
abstract void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) 
Sets the value of a single preference for the rendering algorithms.
abstract void setRenderingHints(Map<?,?> hints) 
Replaces the values of all preferences for the rendering algorithms with the specified hints.
abstract void setStroke(Stroke s) 
Sets the Stroke for the Graphics2D context.
abstract void setTransform(AffineTransform Tx)
Overwrites the Transform in the Graphics2D context.
abstract void shear(double shx, double shy) 
Concatenates the current Graphics2D Transform with a shearing transform.
abstract void transform(AffineTransform Tx) 
Composes an AffineTransform object with the Transform in this Graphics2D according to the rulelast-specified-first-applied.
abstract void translate(double tx, double ty) 
Concatenates the current Graphics2D Transform with a translation transform.
abstract void translate(int x, int y) 
Translates the origin of the Graphics2D context to the point (x, y) in the current coordinate system.
This class inherits methods from the following classes:
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
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

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page