How do I draw a round rectangle in Java 2D?

The RoundRectangle2D class defines a rectangle with rounded corners defined by a location (x,y), dimension (w x h), and the width and height of an arc with which to round the corners.

The RoundRectangle2D.Double class constructs a RoundRectangle2D from the specified values in double, including the location, width and the arch of the round rectangle.

package org.kodejava.example.awt.geom;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

public class DrawRoundRectangle extends JApplet {
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2.0f));
        double x = 50;
        double y = 50;
        double w = x + 250;
        double h = y + 100;
        g2.draw(new RoundRectangle2D.Double(x, y, w, h, 50, 50));

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JApplet applet = new DrawRoundRectangle();
        frame.getContentPane().add(applet, BorderLayout.CENTER);
        frame.setSize(400, 300);

Here is the result you’ll get when you run the snippet:

Draw Round Rectangle Demo

Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply