1,105,416 Community Members

Help in drawing graph

Member Avatar
venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I m developing a java app that will have a GUI to draw the graph and implements the graph coloring algorithm to colour the nodes. I started with JFrame and Canvas and could draw lines and circles with paint() and repaint methods.. The problem is repaint method erases the previous info in the window.. i want to retain the nodes and edges drawn so far and continue drawing new edges.. I tried the overloaded version of repaint that repaints a portion but it is not efective.. i need to draw on the existing lines and nodesby overlapping them.. how can i go about it?

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Override paintComponent, not paint. This restricts your (re)paint to the client area only. Do not override repaint, just call it when necessary.
Do not call super.paintComponent in your paintComponent, because that's where the old content is being cleared. Just go ahead and paint the new stuff.
For more details see the second half of this:
http://java.sun.com/products/jfc/tsc/articles/painting/

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 

Another solution is to create a working image with BufferedImage, draw your lines on the working image and then in the paintComponent method, draw the working image on the screen.
Your previously drawn figures would be preserved in the working image. This approach would be similiar to the double buffering technique.

Member Avatar
venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I changed the paint to paintComponent still repaint clears the screen....

Member Avatar
venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

got it.. I had extended JComponent instead of JPanel.. now it works fine.. Thank you..:)

Member Avatar
venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

new problem has occured.. i defined a class vertex that has

class vertex {
	final Point location;
	int index;
	
      //Constructor
	vertex(Point loc,int ind)
	{
		index=ind;
		LinkedList adjlist=new LinkedList();	
		location=loc;
	}

        //Check whether a point arb is inside this vertex
	boolean haspoint(Point arb)
	{
		if(Math.sqrt(Math.pow(arb.x-(location.x+15),2) + 
                                Math.pow(arb.y-(location.y+15),2))<15)
		return true;
		else
		return false;
		
	}
}

on every mous click im creating a vertex and adding to a LinkedList list..

vertex temp=new vertex(p,count-1); //count-1 is the index
list.addLast(temp);

but finally when i display all the items in the linked list using

for(int i=0;i<list.size();i++)
{
 vertex temp;
 temp=(vertex)list.get(i);
 System.out.println(temp.index+" "+temp.location);
}

all the location points are coming as the same
1 java.awt.Point[x=174,y=259]
2 java.awt.Point[x=174,y=259]
3 java.awt.Point[x=174,y=259]
4 java.awt.Point[x=174,y=259]
5 java.awt.Point[x=174,y=259]

I should get 5 different points but it had stored the location of last click for all the nodes.. pls help...

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: