top of page

Draw a Circle

How to Run It

  1. Save the file below:

    • Name it CircleDrawing.java

  2. Compile the Java code:

    • Open terminal or command prompt in the file’s folder.

    • Run: javac CircleDrawing.java

  3. Run the program:

    • Run: java CircleDrawing

  4. ✅ A window should pop up with a red circle drawn in it.

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

​

public class CircleDrawing extends JPanel {

​

    // Override paintComponent to do custom drawing
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

​

        // Set color to red
        g.setColor(Color.RED);

​

        // Draw a filled circle (oval)
        g.fillOval(100, 100, 200, 200);  // x, y, width, height
    }

    // Main method to set up the window
    public static void main(String[] args) {
        JFrame frame = new JFrame("Circle Drawing");
        CircleDrawing panel = new CircleDrawing();

​

        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);  // Add our custom panel
        frame.setVisible(true);
    }
}

 

bottom of page