views:

1768

answers:

4

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?

A: 

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
-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
Are you kidding me? Then just leave `getGraphics()` and move the code into the `paintComponent` method.
jitter
@camickr. thx to you although I basically gave the same answer as Jack I'm on -1
jitter
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
A: 

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
+1  A: 

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, radius;
  }

  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.radius, c.radius)
  }
}
Jack
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?
i noted in the original post the things that I added
ok i just casted g to Graphics2D. thanks
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
+1  A: 

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