MisterTootor M.S., B.S., A.S., A.S.B
I'm a paragraph. Click here to add your own text and edit me. It's easy.
Draw a Circle
How to Run It
-
Save the file below:
-
Name it CircleDrawing.java
-
-
Compile the Java code:
-
Open terminal or command prompt in the file’s folder.
-
Run: javac CircleDrawing.java
-
-
Run the program:
-
Run: java CircleDrawing
-
-
✅ 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);
}
}