how to make a circle in java code example

Example 1: java create circle

// x position, y position, x size, y size.
ellipse(0, 0, 100, 100);

Example 2: area of circle in java

import java.util.Scanner;
public class AreaOfCircle {
   public static void main(String args[]){
      int radius;
      double area;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the radius of the circle ::");
      radius = sc.nextInt();
      area = (radius*radius)*Math.PI;
      System.out.println("Area of the circle is ::"+area);
   }
}

Example 3: 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