How do I draw an ellipse in Java 2D?

The Ellipse2D class define an ellipse that is defined by a framing rectangle. You can create an ellipse using a double or float values. When creating an ellipse using double values use the Ellipse2D.Double class. And for the float values you can use the Ellipse2D.Float class.

package org.kodejava.example.geom;

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

public class DrawEllipse extends JComponent {
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        g2.setStroke(new BasicStroke(5.0f));
        g2.draw(new Ellipse2D.Double(50, 50, 250, 250));

        g2.fill(new Ellipse2D.Double(10, 10, 40, 40));

        g2.fill(new Ellipse2D.Double(10, 300, 40, 40));

        g2.fill(new Ellipse2D.Double(300, 300, 40, 40));

        g2.fill(new Ellipse2D.Double(300, 10, 40, 40));

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Ellipse Demo");
        frame.getContentPane().add(new DrawEllipse());
        frame.setSize(new Dimension(420, 400));

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

Draw Ellipse Demo

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🙂.


    • Yes, that’s correct. I should have said before that when we use the same value for width and height when creating an Ellipse2D it will simply give us a circle.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.