Java is a programming language that mostly has more than one function or method for executing a wide range of tasks. The creation of a circle is described as one of the examples of such methods and functions. There are various functions that help us create a circle in Java. In this article, we will implement the different built-in functions to draw a circle using Java programming.
How to Draw a Circle in Java?
Drawing a circle in Java is easy by using the built-in packages. In order to draw a circle in Java various methods are available. Among these functions, the fillOval() function, drawRoundRect() function, and draw() function are the most popularly used functions. We will implement the available functions one by one to see the results.
Method 1: Use fillOval() Function to Create a Circle in Java
The fillOval() function helps users to create a circle in Java. It fills the circle with the default/current color. The arguments accepted by the fillOval() function are the x-axis, y-axis, width, and height. Using the fillOval() method with these arguments will draw a circle as depicted below.
//Import the required packages.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class javacircle extends JFrame {
public javacircle() {
setTitle("JAVA PROGRAM TO DRAW A CIRCLE");
// Declare the height and the width of the window.
setSize(400, 400);
// Set the visibility as true.
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// The graphics in Java
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// Set the axis with height and weight.
g2d.fillOval(80, 80, 150, 150);
// Set the color to be black.
g2d.setPaint(Color.black);
}
// The main class in Java.
public static void main(String[] args) {
// Calling the object created to display the circle.
javacircle frames = new javacircle();
frames.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Set the size of the frame.
frames.setSize(400, 400);
// Set the visibility of the frame.
frames.setVisible(true);
}
}
In the above Java code:
- The required packages are imported.
- The properties of the frame are set respectively to display the circle on the frame.
- The graphics class is declared to get the properties required.
- The fillOval() function has an x and y axis and the height and width are declared.
- The action listener class of Java is used along with the required parameters.
Output
The filled circle within a frame has been successfully created in Java using the fillOval() function.
Method 2: drawRoundRect() Function to Create a Circle in Java
The drawRoundRect() function takes six arguments and creates the circle accordingly. The arguments include the x and y axis, the width and the height, and the height and the width of the arc. Below is the code implementation of the function
//Import the required packages.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class javacircle extends JFrame {
public javacircle() {
setTitle("JAVA PROGRAM TO DRAW A CIRCLE USING ROUNDRECT");
// Declare the height and the width of the window.
setSize(400, 400);
// Set the visibility as true.
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// The graphics in Java
@Override
public void paint(Graphics g) {
Graphics2D d = (Graphics2D) g;
// Set the axis with height and weight along with the height and width of the arc.
d.drawRoundRect(50, 50, 150, 150, 200, 200);
}
// The main class in Java.
public static void main(String[] args) {
// Calling the created object to display the circle
javacircle frames = new javacircle();
frames.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Set the size of the frame.
frames.setSize(400, 400);
// Set the visibility of the frame.
frames.setVisible(true);
}
}
The above code is exactly the same as the previous one except for the difference that the function drawRoundRect() is used instead of fillOval().
Output
The output below comprises a circle using the drawRoundRect() function in Java.
Method 3: Use Shape Class and draw() Function to Create a Circle in Java
The shape class can be used with the height, width, and axis parameters to define the shape of the object. The draw() function accepts a shape as an argument and creates the shape accordingly. The Java code is implemented below.
//Import the required packages.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
//Create a class that extends another class.
class javacircle extends JFrame {
public javacircle() {
setTitle("use Shape and draw() to create a circle");
// Declare the height and the width of the window.
setSize(400, 400);
// Set the visibility as true.
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// The graphics in Java
@Override
public void paint(Graphics g) {
Graphics2D d = (Graphics2D) g;
//using the shape function
Shape circle = new Ellipse2D.Double(80, 80, 150, 150);
//using the draw function.
d.draw(circle);
}
// The main class in Java.
public static void main(String[] args) {
// Calling the created object to display the circle.
javacircle frames = new javacircle();
frames.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Set the size of the frame.
frames.setSize(400, 400);
// Set the visibility of the frame.
frames.setVisible(true);
}
}
The above code is executed using the shape and draw() function in Java.
Output
In the output below the circle is designed using the shape and draw() functions of Java.
This article has effectively demonstrated three different methods for circle creation in Java.
Conclusion
To create a circle in Java there are various functions available. These functions are the fillOval() function, the drawRoundRect() function, and the draw() function. These functions use certain arguments like height, width and the axis to create a circle in Java. In this article, we have implemented various functions to create circles in Java.