2

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?

user69514
  • 26,935
  • 59
  • 154
  • 188

4 Answers4

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);
jitter
  • 53,475
  • 11
  • 111
  • 124
  • -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. – camickr Dec 02 '09 at 23:03
  • Are you kidding me? Then just leave `getGraphics()` and move the code into the `paintComponent` method. – jitter Dec 02 '09 at 23:49
  • @camickr. thx to you although I basically gave the same answer as Jack I'm on -1 – jitter Dec 03 '09 at 02:01
  • 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. – camickr Dec 03 '09 at 02:18
  • +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. – Erik S Mar 12 '12 at 20:15
  • how to fill it? – toha Sep 17 '16 at 16:16
  • to fill it using g.fillOval(x, y, radius, radius); – toha Sep 17 '16 at 16:18
2

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)
  }
}
silvalli
  • 295
  • 2
  • 13
Jack
  • 131,802
  • 30
  • 241
  • 343
  • 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 Dec 02 '09 at 23:52
  • i noted in the original post the things that I added – user69514 Dec 02 '09 at 23:54
  • 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. – Jack Dec 03 '09 at 00:25
1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

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

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449