Draw Rounded Rectangle Using Graphics Object in Java



In this article, we will learn to draw a rounded rectangle using the Graphics object in Java. Drawing shapes is a basic feature of Java 2D graphics programming. Though the Graphics class offers methods for drawing common rectangles, drawing rounded rectangles involves some special techniques.

Graphics Class

In Java, the drawing takes place via a Graphics object, which is an instance of the java.awt.Graphics class. Each Graphics object has its own coordinate system, and all the methods of Graphics, including those for drawing Strings, lines, rectangles, circles, polygons, etc. We can get access to the Graphics object through the paint(Graphics g) method.

Differernt apppraches

The following are the two different approaches to draw a rounded rectangle using the Graphics object in Java:

Using the drawRoundRect() Method

We can use the drawRoundRect() method that accepts x-coordinate, y-coordinate, width, height, arcWidth, and arcHeight to draw a rounded rectangle. The drawRoundRect() method is designed for drawing the outline of a rounded rectangle.

Syntax

The following is the syntax of drawRoundRect initialization:

g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);

Approach

The following are the steps to draw a rounded rectangle using the drawRoundRect() method:

  • Creating the JFrame: We begin by creating a JFrame, which will be our app's main window.
  • Setting Up the JFrame: Windows title, size, close operation, as well as centering on the screen, will be set by the methods setTitle(), setSize(), setDefaultCloseOperation(), setLocationRelativeTo().
  • Overriding the paint Method: This means complementing the paint method so that it is possible to define what would get drawn inside the window.
  • Drawing the Rounded Rectangle: In the paint method, we use a Graphics2D object and call drawRoundRect() to draw a rectangle that has rounded corners.
Graphics2D g2d = (Graphics2D) g;
g2d.drawRoundRect(10, 50, 150, 150, 50, 30);

Example

Below is an example of drawing a rounded rectangle using the drawRoundRect() method:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RoundedRectangleTest extends JFrame {
   public RoundedRectangleTest() {
      setTitle("RoundedRectangle Test");
      setSize(350, 275);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      g2d.drawRoundRect(10, 50, 150, 150, 50, 30);
   }
   public static void main(String []args) {
      new RoundedRectangleTest();
   }
}

Ouptut

Using fillRoundRect() Method

The fillRoundRect() method creates solid, filled, rounded rectangles. This approach is particularly useful for creating solid background elements and developing buttons and interactive components.

Syntax

The following is the syntax of fillRoundRect initialization:

g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);

Approach

The following are the steps to draw a filled, rounded rectangle using the fillRoundRect() method:

  • Creating the JFrame: We begin by creating a JFrame, which will be our app's main window.
  • Setting Up the JFrame: Windows title, size, close operation, as well as centering on the screen, will be set by the methods setTitle(), setSize(), setDefaultCloseOperation(), setLocationRelativeTo().
  • Overriding the paint Method: This means complementing the paint method so that it is possible to define what would get drawn inside the window.
  • Drawing the filled Rounded Rectangle: Inside the paint method, we use a Graphics2D object and call the fillRoundRect() method to draw a filled rectangle with rounded corners and set its inside color as Orange using the setColor() method.
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.ORANGE);
g2d.fillRoundRect(10, 50, 150, 150, 50, 30);

Example

Below is an example of drawing a filled, rounded rectangle using the fillRoundRect() method:

import java.awt.*; 
import javax.swing.*;
public class FilledRoundedRectangleTest extends JFrame {
   public FilledRoundedRectangleTest() {
      setTitle("RoundedRectangle Test");
      setSize(350, 275);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }  
   public void paint(Graphics g) {
      super.paint(g);
      Graphics2D g2d = (Graphics2D) g;
      g2d.setColor(Color.ORANGE);
      g2d.fillRoundRect(10, 50, 150, 150, 50, 30);
   }  
   public static void main(String[] args) {
      new FilledRoundedRectangleTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-29T19:12:17+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements