How do I draw an arc in Java 2D?

Arc2D is the abstract superclass for all objects that store a 2D arc defined by a framing rectangle, start angle, angular extent (length of the arc), and a closure type
(Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE).

To constructs a new arc in double values, such as defining the specified location, size, angular extents, and closure type we can use the Arc2D.Double static-inner class.

package org.kodejava.example.awt.geom;

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

public class DrawArc extends JApplet {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        g2.setStroke(new BasicStroke(2.0f));
        g2.setPaint(Color.RED);
        g2.draw(new Arc2D.Double(50, 50, 300, 300, 0, 90, Arc2D.PIE));
        g2.setPaint(Color.GREEN);
        g2.draw(new Arc2D.Double(50, 50, 300, 300, 90, 90, Arc2D.PIE));
        g2.setPaint(Color.BLUE);
        g2.fill(new Arc2D.Double(50, 50, 300, 300, 180, 90, Arc2D.PIE));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Arch Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JApplet applet = new DrawArc();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setSize(new Dimension(400, 450));
        frame.setVisible(true);
    }
}

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

Draw Arch Demo

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 {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        
        g2.setPaint(Color.RED);
        g2.setStroke(new BasicStroke(5.0f));
        g2.draw(new Ellipse2D.Double(50, 50, 250, 250));

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

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

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

        g2.setPaint(Color.ORANGE);
        g2.fill(new Ellipse2D.Double(300, 10, 40, 40));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Ellipse Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JApplet applet = new DrawEllipse();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setSize(new Dimension(400, 400));
        frame.setVisible(true);
    }
}

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

Draw Ellipse Demo

How do I draw a GeneralPath in Java 2D?

package org.kodejava.example.awt.geom;

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

public class DrawGeneralPath extends JApplet {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        
        g2.setStroke(new BasicStroke(4.0f));
        g2.setPaint(Color.GREEN);

        int[] xPoints = {10, 50, 100, 150, 200, 250, 300, 350};
        int[] yPoints = {10, 50,  10,  50,  10,  50,  10,  50};
        GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                xPoints.length);

        //
        // Adds point to the path by moving to the specified
        // coordinates.
        //
        path.moveTo(xPoints[0], yPoints[0]);
        for (int i = 1; i < xPoints.length; i++) {
            //
            // Adds a point to the path by drawing a straight
            // line from the current position to the specified
            // coordinates.
            //
            path.lineTo(xPoints[i], yPoints[i]);
        }
        path.curveTo(150, 150, 300, 300, 50, 250);
        path.closePath();
        g2.draw(path);

        //
        // Draw another path, a start
        //
        g2.setPaint(Color.RED);
        g2.setStroke(new BasicStroke(2.0f));
        path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
        path.moveTo(200, 50);
        path.lineTo(270, 300);
        path.lineTo(100, 120);
        path.lineTo(300, 120);
        path.lineTo(130, 300);
        path.closePath();
        g2.draw(path);

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw GeneralPath Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JApplet applet = new DrawGeneralPath();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setSize(new Dimension(400, 400));
        frame.setVisible(true);
    }
}

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

Draw General Path

How do I draw a line in Java 2D?

The following code snippet show you how to draw a simple line using Graphics2D.draw() method. This method take a parameter that implements the java.awt.Shape interface.

To draw a line we can use the Line2D.Double static-inner class. This class constructor takes four integers values that represent the start (x1, y1) and end (x2, y2) coordinate of the line.

package org.kodejava.example.awt.geom;

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

public class DrawLine extends JApplet {
    @Override
    public void paint(Graphics g) {
        //
        // Draw a simple line using the Graphics2D draw() method.
        //
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2f));
        g2.setColor(Color.RED);
        g2.draw(new Line2D.Double(50, 150, 250, 350));
        g2.setColor(Color.GREEN);
        g2.draw(new Line2D.Double(250, 350, 350, 250));
        g2.setColor(Color.BLUE);
        g2.draw(new Line2D.Double(350, 250, 150, 50));
        g2.setColor(Color.YELLOW);
        g2.draw(new Line2D.Double(150, 50, 50, 150));
        g2.setColor(Color.BLACK);
        g2.draw(new Line2D.Double(0, 0, 400, 400));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Line");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JApplet applet = new DrawLine();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setSize(new Dimension(420, 440));
        frame.setVisible(true);
    }
}

When you run the snippet it will show you something like:

Draw Line Applet

How do I draw a rectangle in Java 2D?

The code snippet below show you how to use the Graphics2D class the draw a rectangle. You can see the snippet in the paintComponent(Graphics g) method defined in the anonymous JPanel object.

To draw a rectangle we use the Rectangle2D.Double static-inner class. The constructor of this class accept the information about the rectangle x, y coordinates and its witdh and height.

package org.kodejava.example.awt.geom;

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

public class DrawRectangle {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Draw Rectangle");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.add(new JPanel() {
                    @Override
                    protected void paintComponent(Graphics g) {
                        //
                        // Draw a rectangle using Rectangle2D class
                        //
                        Graphics2D g2 = (Graphics2D) g;
                        g2.setColor(Color.RED);

                        double x = 100;
                        double y = 100;
                        double width = x + 200;
                        double height = y + 50;
                       
                        //
                        // Draw the red rectangle
                        //
                        g2.draw(new Rectangle2D.Double(x, y, width, height));

                        g2.setColor(Color.BLUE);
                        //
                        // Draw the blue rectangle
                        //
                        g2.draw(new Rectangle2D.Double(150, 50, 200, 250));
                    }
                }, BorderLayout.CENTER);

                frame.pack();
                frame.setSize(new Dimension(500, 400));
                frame.setVisible(true);
            }
        });
    }
}

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

Draw Rectangle Demo