Table of contents
1.
Introduction 
1.1.
Hierarchy of Graphics Class
1.2.
Example 1
1.3.
Implementation 
1.4.
Output 
1.5.
Example 2
1.6.
Implementation
1.7.
Output 
2.
Frequently Asked Questions📚
2.1.
In Java Swing, what is graphics?
2.2.
Is it possible in Java to use graphics?
2.3.
What is the concept of a Graphics object?
2.4.
What does the Graphics Class Package require?
2.5.
What are the different types of graphics modes?
3.
Conclusion
Last Updated: Sep 19, 2025
Medium

Graphics in java swing

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

The Graphics class allows you to draw strings, lines, shapes, and images onto java components such as a Jpanel. This is accomplished by overriding the paintComponent(Graphics g) method of the JComponent you're drawing on. These Graphics class which are used to create Swing graphics serves as the abstract base class for all graphics programs, allowing them to render components that are compatible with different devices.  

The state information required for the basic rendering operations supported by Java is grouped (combined) by the Java Graphics objects. Some of the properties are included below in the state information. The java.awt.Graphics package includes Swing Graphics. Rectangles, lines, ovals, and other shapes are drawn using these graphics.The current color, clip, font, and logical pixel operations like XOR and Paint are all currently active. In this article we will study Graphics in Java Swing in detail.

Hierarchy of Graphics Class

Drawing on the screen is possible due to the graphics context. The graphics context keeps track of the previously mentioned states, such as the font and colour used in drawing, as well as communicating with the underlying operating system. Custom painting is provided in Java via the java.awt.Graphics class. We must import this package in order to use it. This package also includes graphics-managed methods as well as device-independent methods for drawing texts, figures, and images.

Graphics class methods that are frequently used:🟩🟡🔼

drawString(String strg, int a, int b): Its purpose is to draw a string within the coordinates.

drawRect(int a, int b, int width, int height): It's used to create a rectangle with a specific width and height.

drawLine(int a1, int b1, int a2, int b2): It's used to draw a line with the points a1, b1, and a2, b2.

drawImage(Image pic, int a, int b, ImageObserver observer): It is used to draw the image.

drawArc(int a, int b, int width, int height, intstartAngle, intarcAngle): It's a tool for drawing elliptical or circular shapes.

fillArc(int a, int b, int width, int height, intstartAngle, intarcAngle): It's used to fill the arc of a circular or elliptical shape.

setColor(Color clr): It’s used to set color.

setFont(Font fnt): To set specified font it is used.

fillRect(int a, int b, int width, int height):This is used to fill the rectangle with the specified color using the provided coordinates and width and height.

drawOval(int a, int b, int width, int height): It's used to create an oval shape with the width and height specified.

fillOval(int a, int b, int width, int height): It's used to fill the oval shape with the colour and width and height that you choose.

Example 1

Now that we have discussed different types of graphics class methods let us discuss this example where we will discuss how to draw different shapes using the drawString, drawOval and drawArc method.

  • First of all you need to import all the toolkits and required packages for graphics such as import java.awt.* and import javax.swing.JFrame as you can see in the given code. 
  • Then you need to extend Canvas class into our own class.
  • You call methods on a Graphics object to draw on the canvas. The Graphics object is created automatically when the Canvas is formed and is supplied as an argument to paint, so you don't need to construct it.
  • Now you can play with different graphics class methods as discussed to form the desired shapes.
  • At the end the Line drawn is made visible by making setVisible() as true.

Implementation 

import java.awt.*;
import javax.swing.JFrame;
public class GraphicsDisplay extends Canvas {
  public void paint(Graphics x) {
    //adding the string to graphics
    x.drawString("Hello", 40, 40);
    //setting background color as white
    setBackground(Color.WHITE);
    //Drawing rectangle shape
    x.fillRect(130, 30, 100, 80);
    x.drawOval(30, 130, 50, 60);
    setForeground(Color.RED);
    x.fillOval(130, 130, 50, 60);
    x.drawArc(30, 200, 40, 50, 90, 60);
    x.fillArc(30, 130, 40, 50, 180, 40);
  }
  public static void main(String[] str) {
    //adding graphics to the frame to display
    GraphicsDisplay n = new GraphicsDisplay();
    JFrame y = new JFrame();
    n.add(y);
    n.setSize(400, 400);
    //f.setLayout(null);  
    n.setVisible(true);
  }
}

Output 

Example 2

I hope you found the above discussed example interesting and easy. Now let us see  this example where we will discuss how to draw lines using the drawLines method.

  • First of all you need to import all the toolkits and libraries as you can see in the given code. 
  • You can also use @SuppressWarnings(“serial”) to get rid of the warning messages. You need to set the title and size of the line. 
  • Now we need to use the drawLines method in which we will create a 2D graphic and draw different types of expected lines by giving them dimensions. 
  • At the end the Line drawn is made visible by making setVisible() as true.

Implementation

//package com.graphics.swing;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
// A javax.swing.JFrame extension in a Swing application.
@SuppressWarnings("serial")
public class DrawingLine extends JFrame {
  public DrawingLine() {
    //Setting the title 
    super("Drawing Lines");
    //setting the size 
    setSize(300, 100);
    //After the application is closed, set exit on close.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
  }
  //drawing lines using the drawLines method
  void drawLines(Graphics graphics) {
    //creating a 2d graphic
    Graphics2D graphics2d = (Graphics2D) graphics;
    //drawing  single line
    graphics2d.drawLine(120, 50, 360, 50);
    //drawing  double lines
    graphics2d.draw(new Line2D.Double(59.3 d, 99.9 d, 419.2 d, 99.89));
    //drawing a floating line
    graphics2d.draw(new Line2D.Float(21.51 f, 132.51 f, 459.51 f, 132.51 f));
  }
  //painting graphics
  public void paints(Graphics graphics) {
    super.paints(graphics);
    //calling the paint method's drawLines method
    drawLines(graphics);
  }
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        //setting LinesDrawing visible
        new LinesDrawingLine().setVisible(true);
      }
    });
  }
}

Output 

Frequently Asked Questions📚

In Java Swing, what is graphics?

A Graphics object encapsulates the state information required for Java's fundamental rendering operations. The following properties are included in this state information: The Component object that will be used to draw on. For rendering and clipping coordinates, a translation origin is used.

Is it possible in Java to use graphics?

The simplest method is to use java. awt. Canvas and java. Awt.

What is the concept of a Graphics object?

The Graphics object is used to create graphical images, and it represents a GDI+ drawing surface. When dealing with graphics, there are two steps: Creating a Graphics object is the first step. To draw lines and shapes, produce text, or display and edit pictures, use the Graphics object.

What does the Graphics Class Package require?

The awt (abstract windowing toolkit) package contains the Graphics class. This class holds information about the current graphics 'context.' The current drawing colour, typeface, and transformation information are all included in the context.

What are the different types of graphics modes?

Graphics mode is a computer display mode that uses pixels to create images. Most people nowadays use their computers in graphical mode rather than text mode or command line mode.

Conclusion

As discussed in the blog, the different types of object shapes can be drawn on the screen using Swing Graphics. In this article, we have also discussed different graphics class methods that can be used to define different properties. We hope that this article has helped you enhance your knowledge of graphics in Java Swing. 

Recommended Readings:

Upcasting and Downcasting in Java

Generics in JavaTrim in Java 

Java Regex.

 

 

 

Live masterclass