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.awt.geom;

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

public class DrawEllipse extends JApplet {
    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");

        JApplet applet = new DrawEllipse();
        frame.setSize(new Dimension(400, 400));

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

Draw Ellipse 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


  1. Change your width to be smaller than your height. Think of it as stretching your circle vertically without changing the width.

  2. 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