Drawing a Ractangle with rounded corners using Java 2D Graphics API


To draw a rectangle with rounded corners using the Java 2D graphics API is much similar to drawing a normal rectangle.
The only difference is that the static Float method takes additionally two arguments. Consider the code below:


    public void paint(Graphics g) {
        Graphics2D graphics2 = (Graphics2D) g;
        RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(100, 100, 240, 160, 10, 10);
        graphics2.draw(roundedRectangle);
    }


The Float() method of the class RoundRectangle2D takes six arguments.
The first two represents the location of the upper left corner. Argument 3 and 4 represents the width and height of the rounded rectangle.
The last two arguments represent the width and height of the arc drawn in the corners.
This is what it looks like when drawn on a JFrame:

Ractangle with rounded corners drawn using Java 2D Graphics API

This is what the complete code of the JFrame class looks like:


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
/**
 * Displays a JFrame and draws a rounded ractangle on it using the Java 2D Graphics API
 *
 * @author www.javadb.com
 */

public class Java2DFrame extends javax.swing.JFrame {
    
    /**
     * Creates a new instance of Java2DFrame
     */

    public Java2DFrame() {
        initComponents();
    }
    
    /**
     * This is the method where the rounded rectangle is drawn.
     *
     * @param g The graphics object
     */

    public void paint(Graphics g) {
        Graphics2D graphics2 = (Graphics2D) g;
        RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(100, 100, 240, 160, 10, 10);
        graphics2.draw(roundedRectangle);
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>
    
    /**
     * Starts the program
     *
     * @param args the command line arguments
     */

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Java2DFrame().setVisible(true);
            }
        });
    }
}

See also Drawing a Ractangle using Java 2D Graphics API
Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: