add and remove ellipse2D from instance of JPanel object

Posted on 2012-09-02
Last Modified: 2013-01-25

I have a instance of a JPanel class where i draw an elipse. What i'm trying to do is to add and remove more ellipses from the JPanel object from outside the class.

Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.geom.Line2D.Double;
import java.util.concurrent.*;

public class DrawBugElement extends JPanel{
	int posX = 0;
	int posY = 0;
	public DrawBugElement (int posX, int posY){
		this.posX = posX;
		this.posY = posY;
	public void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		Ellipse2D bug = new Ellipse2D.Double(this.posX, this.posY, Main.scale, Main.scale);
	public void setPos(int posX, int posY){
		this.posX = posX;
		this.posY = posY;

Open in new window

And here is the code where i would like to add more ellipses from (The constructor of a much bigger class). See the comment:
public Bug(Chromosome chromosome, int posX, int posY){
		this.chromosome = chromosome;
		this.posX = posX;
		this.posY = posY;
                bugElement gridElement = new DrawBugElement ();
		bugElement .posX = this.posX;
		bugElement .posY = this.posY;
                //Here i would like to do something like:
                //gridElement.add(new Ellipse2D.Double(50, 50, 50, 50));
		Main.mainFrame.add(bugElement );

Open in new window

I cant figure out how to do this?
Question by:Haugenwebdesign
    LVL 86

    Accepted Solution

    You could do something like

    a. give Bug a method
    public draw (Graphics g);

    Open in new window

    b. In DrawBugElement:

    private List<Bug> bugs;
    public void addBug(Bug b) {
       if(bugs == null) {
          bugs = new ArrayList<Bug>(); // lazy init
    public void paintComponent(Graphics g){
       for(Bug b : bugs) {

    Open in new window

    LVL 53

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    hash value 2 29
    Python Encoding Problem \u2013 4 57
    network + 7 58
    parentbit challenge 3 37
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now