How to draw a circle in NetBeans

The NetBeans IDE (Integrated Development Environment) facilitates Java programming. When you design and build a Java GUI (Graphical User Interface) in NetBeans, the IDE automates certain aspects of the coding, including user interface elements such as buttons and labels. However, for certain user interface tasks, you need to implement the code yourself. This includes graphics tasks such as drawing a circle. The code required to draw a circle in Java is relatively straightforward and can be added to your existing NetBeans projects if necessary.

Open your NetBeans project. If you have already created a GUI in your application, for example with a JFrame, open the class for it now. To build a GUI window from scratch, create a new class in your application, using the following outline for a JFrame: import javax.swing.*; public class LovelyGUI extends JFrame { public LovelyGUI(){ //set basic window properties setTitle("Lovely Window"); setSize(300, 300); setLocation(250, 250); setVisible(true); } }

This code defines a window with basic properties. To display the window, include the following in your project main method: LovelyGUI niceGUI = new LovelyGUI();

Create a JPanel class. To draw a circle, define your own JPanel class to add to the GUI. Create a new class in your Java application project in NetBeans and enter the following outline: import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class LovelyPanel extends JPanel { //class content }

This defines a custom JPanel in which you can tailor the details to suit your own application. To add it to your GUI class, include the following line before the line in which you called "setVisible" to display the window: LovelyPanel nicePanel = new LovelyPanel(); add(nicePanel, "Center");

This will add the panel to the centre of the window.

Implement graphics control. In your JPanel class, you can carry out drawing operations by overriding the "paintComponent" method. Add its outline inside the JPanel class as follows: @Override public void paintComponent(Graphics g) { //draw shapes }

Inside this method, you can define drawing and painting operations to determine what appears within the JPanel when it is shown inside the GUI.

Draw a circle in the JPanel. Inside your JPanel "paintComponent" method, draw a circle as follows: g.drawOval(10, 10, 100, 100);

This uses the passed Graphics object parameter to carry out the drawing process. The first two parameters to the "drawOval" method represent the co-ordinates of the upper left corner of the space in which the oval is to be drawn within the panel. The second two parameters represent the width and height of the oval. The oval in this case is a circle, so the width and height are equal.

Customise your circle. You can add various optional lines of code before the line in which you draw the oval shape to tailor the appearance of the circle. The following example code alters the colour in which the oval will be drawn: g.setColor(Color.MAGENTA);

The Graphics object provides options for such properties as clipping, filling and overwriting.


The Graphics class provides a range of other shapes you can draw, including lines, polygons, rectangles, arcs and 3D rectangles.


Achieving the GUI design you want in Java can require a lengthy process of trial and error.

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Sue Smith started writing in 2000. She has produced tutorials for companies including Apex Computer Training Software and articles on computing topics for various websites. Smith has a Master of Arts in English language and literature, as well as a Master of Science in information technology, both from the University of Glasgow.