How to draw circle on JPanel? Java 2D

62,629

Solution 1

Your approach can be something similar to this, in which you use a class CircleList to hold all the circles and the drawing routine too:

class CircleList
{
  static class Circle
  {
    public float x, y, diameter;
  }

  ArrayList<Circle> circles;

  public CirclesList()
  {
    circles = new ArrayList<Circle>();
  }

  public void draw(Graphics2D g) // draw must be called by paintComponent of the panel
  {
    for (Circle c : circles)
      g.fillOval(c.x, c.y, c.diameter, c.diameter)
  }
}

Solution 2

Easiest thing to do would be to place something along these lines into your paintComponent method.

int x = ...;
int y = ...;
int radius = ...;
g.drawOval(x, y, radius, radius);

Solution 3

Have you looked at JXLayer? It's an awesome library that allows you to layer special painting on top of any GUI element in an obvious way. I believe that will be included in the main java libraries for JDK7

Solution 4

Well, you will probably want to create an ArrayList to store the information about the circles to be drawn. Then when the paintComponent() method is invoked you just loop through the ArrayList and draw the circles.

Custom Painting Approaches shows how this might be done for a rectangle. You can modify the code for an oval as well you would probably add methods to update the Array with the location information rather than by doing it dynamically.

Share:
62,629
user69514
Author by

user69514

Updated on July 09, 2022

Comments

  • user69514
    user69514 almost 2 years

    I have a JPanel for which I set some image as the background. I need to draw a bunch of circles on top of the image. Now the circles will be positioned based on some coordinate x,y, and the size will be based on some integer size. This is what I have as my class.

    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.JPanel;
    
    class ImagePanel extends JPanel {
    
        private Image img;
        CircleList cList;  //added this
    
        public ImagePanel(Image img) {
            this.img = img;
            Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
            setLayout(null);
    
            cList = new CircleList(); //added this
        }
    
        public void paintComponent(Graphics g) {
            g.drawImage(img, 0, 0, null);
    
            cList.draw(null); //added this
        }
    }
    

    How can I create some method that can performed this?

  • camickr
    camickr over 14 years
    -1 Using getGraphics() will NOT work. Yes it will draw the oval once, but it will be removed as soon as JAVA determines the panel needs to be repainted. Custom painting is done by override the paintComponent() method.
  • cal meacham
    cal meacham over 14 years
    Are you kidding me? Then just leave getGraphics() and move the code into the paintComponent method.
  • user69514
    user69514 over 14 years
    Ok so I created I CircleList with two arbitrary circles in the linked list. When I call circleList.draw(null) inside the ImagePanel class nothing happens. The GUI actually freezes. What's the parameter for the draw method when I call it?
  • user69514
    user69514 over 14 years
    i noted in the original post the things that I added
  • Jack
    Jack over 14 years
    graphics2D was the one.. the same graphics component of paintComponent of JPanel. I'm used always to cast it to Graphics2D because it's more powerful.
  • cal meacham
    cal meacham over 14 years
    @camickr. thx to you although I basically gave the same answer as Jack I'm on -1
  • camickr
    camickr over 14 years
    You gave wrong or misleading information. If I had not commented on this, then other people who read the posting might think using the getGraphics() method is a cool idea. When newbies have to ask a basic question like this they should be given information about the proper way to do things.
  • Erik S
    Erik S about 12 years
    +1'd to get you on 0 since you edited the answer and it now gives accurate tips. Hell yea for changing things in dead posts.
  • toha
    toha over 7 years
    how to fill it?
  • toha
    toha over 7 years
    to fill it using g.fillOval(x, y, radius, radius);