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?
Jump to Post
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 …
Jump to Post
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 …
All 5 Replies
We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.