How to draw a circle in NetBeans

Written by sue smith Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to draw a circle in NetBeans
With Java you can define the graphical elements of Windows your application presents to users. (BananaStock/BananaStock/Getty Images)

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.

Skill level:


  1. 1

    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();

  2. 2

    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.

  3. 3

    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.

  4. 4

    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.

  5. 5

    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.

Tips and warnings

  • 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.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.