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?

Recommended Answers

All 5 Replies

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/

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.

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

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

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.