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?

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;
	vertex(Point loc,int ind)
		LinkedList adjlist=new LinkedList();	

        //Check whether a point arb is inside this vertex
	boolean haspoint(Point arb)
		if(Math.sqrt(Math.pow(arb.x-(location.x+15),2) + 
		return true;
		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

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

for(int i=0;i<list.size();i++)
 vertex temp;
 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...

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

This article has been dead for over six months. Start a new discussion instead.