Graphics2D is the part of the Java 2D API related to two-dimensional graphics, text, and imaging capabilities. Unlike the older Graphics class, Graphics2D supports coordinate transforms. It also gives better control over geometry, colors and text layout.
The java.awt.Graphics2D Java class is part of the Abstract Windowing Toolkit (awt) library and was first included in Java Standard Edition version 1.4. It supersedes the java.awt.Graphics class from earlier Java versions. Graphics and Graphics2D are subsequently used by swing.
Unlike Graphics, Graphics2D supports coordinate transforms, uses floats rather than ints for better control over geometry, also has improved color management and text layout control. Many system library methods that return, expect or receive a Graphics object are actually working with Graphics2D object that can be casted:
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
....
}
This allows some restricted Java implementations (like early versions of GNU Classpath) to opt not to implement Graphics2D and still formally support the complete API.
Graphics2D is used to display shapes, text and images in user interfaces components and includes methods for settings the style and transformations of the graphics output, and for controlling the rendering of the graphics.