Share & learn

Through Innovative Digital Library

How to draw circle in Java?

Write a piece of code in Java to draw a circle.
 



Answers

Posted by Paul on April 23rd 2018 13:44 PM

Program to draw a circle in Java : In Java circle can be drawn using java.awt.shape package provided by Java. In this program you will get to know how to draw circle and fill it with color. In program a class is defined as DrawCircle for the circle. This code makes use of Ecllpise2D i.e. in order to specify eclipse float and double. Method : setPaint(): In order to fill the color in circle, this method is used. Here is the code :

// Java program to draw circle
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class 
DrawCircle extends Frame {
    
    
// create object of Shape class 
    
Shape circle = new Ellipse2D.Float(100.0f100.0f100.0f100.0f);
    
    
// method to draw circle
    
public void paint(Graphics g) {
        
        
Graphics2D graphics = (Graphics2Dg;
        
graphics.draw(circle);
        
graphics.setPaint(Color.black);
        
graphics.fill(circle);
    }

    public static 
void main(String args[]) {
        
        
// create a new frame
        
Frame frame = new DrawCircle();
        
        
frame.addWindowListener(new WindowAdapter() {
            public 
void windowClosing(WindowEvent we) {
                
System.exit(0);
            }
        });
        
        
// set properties of frame
        
frame.setSize(350250);
        
frame.setVisible(true);
    }

}

Posted by Paul on April 23rd 2018 13:45 PM

Program to draw a circle in Java : Here is another method of drawing a circle using Swing and AWT. Files needed for this program. 1. java.awt.Graphics 2. javax.swing.JFrame 3. java.awt.Color Methods needed from this program : 1. drawOval(): Used to draw the Shape of the Circle / Oval. 2. setColor(): Used to set the Color to be filled inside the Circle / Oval. 3. fillOval(): Used to fill the Color within the Oval shape. Here is the code :

// Java program to draw circle
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Color;
 
public class 
DrawCircle extends JFrame 
{
    public 
DrawCircle()
    {
        
setTitle("Itsprings");
        
setSize(400,400);
        
setVisible(true);
        
setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
 
    public 
void paint(Graphics g)
    {
        
g.setColor(Color.BLACK);
        
g.drawOval(80,80,200,200);
        
g.fillOval(8080200200);
    }
 
    public static 
void main(String args[])
    {
        
DrawCircle t = new DrawCircle();
        
t.paint(null);

    }

}
Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
W1WB

related questions

Most liked questions

Most answered questions

Login
W1WB Refresh
Register
W1WB Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
W1WB Refresh