We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,018 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help in drawing graph

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?

3
Contributors
5
Replies
17 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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/

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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...

venktech
Newbie Poster
7 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0773 seconds using 2.67MB