how to draw a circle in java using loops code example

Example 1: how to create a circle in java

public class scratch {
    public static void main(String[] args) {
        JFrame window = new JFrame();
        window.setTitle("Christmas Tree");
        window.setSize(700, 600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
        DrawingComponent DC = new DrawingComponent();
        window.add(DC);
    }
}
class DrawingComponent extends JComponent{
      public void paint(Graphics graph0){
        Graphics2D graph = (Graphics2D) graph0;
        Ellipse2D.Double circle = new Ellipse.Double(5, 5, 25, 25);
        graph.fill(circle);
      }
}

Example 2: draw circle in java

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class DrawCircle extends Frame 
{
	// input the value for circle and square.
	Shape circle=new Ellipse2D.Float(100.0f,100.0f,100.0f,100.0f);
	
	// class paint to fill color in circle.
	public void paint(Graphics g)
	{
		Graphics2D ga=(Graphics2D)g;
		ga.draw(circle);
		ga.setPaint(Color.blue);
		ga.fill(circle);
	}
		
	public static void main(String args[])
	{
		// create a frame object for circle.
		Frame frame=new DrawCircle();
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
		    }
		});
		// circle coordinates.
		frame.setSize(300, 250);
		frame.setVisible(true);	
	}
}

Tags:

Java Example