AWT Graphics2D Class

Advertisements


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
1Graphics2D()
Constructs a new Graphics2D object.

Class methods

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

Advertisements
Advertisements