?
Solved

add and remove ellipse2D from instance of JPanel object

Posted on 2012-09-02
3
Medium Priority
?
403 Views
Last Modified: 2013-01-25
Hello.

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){
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		Ellipse2D bug = new Ellipse2D.Double(this.posX, this.posY, Main.scale, Main.scale);
		g2.fill(bug);
	}
	public void setPos(int posX, int posY){
		this.posX = posX;
		this.posY = posY;
		repaint();
	}
	
}

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?
0
Comment
Question by:Haugenwebdesign
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 38358605
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
   }
   bugs.add(b);
}
public void paintComponent(Graphics g){
   super.paintComponent(g);
   for(Bug b : bugs) {
      b.draw(g);
   }
}
...

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 38818107
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses
Course of the Month17 days, 11 hours left to enroll

830 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