Drawing a graph with Swing: general approach
Suppose I have a graph composed of nodes and edges, and I want to draw it in a Swing app. I'm not a Swing programmer, but as far as I know, I see two approaches:
- draw the entire graph as a component
- draw each node and edge as a single component
I've seen an application doing the first. To drag a node, drawn as a circle, the app checks what is the nearest node to the click开发者_开发知识库ed point. It seems to me that this is not much efficient. Is the second approach feasible? And which one should be followed, and why?
We write and maintain JGraph
and after 10 years of doing so we still have a single flyweight renderer that's shared to draw all cells. The reason is the memory cost of a component per cell. The disadvantage is that you need to deal with refreshing and event handling yourself, but it's not that bad.
We've had the coversation many many times, always the same conclusion.
Either approach is feasible, depending on the requirements. GraphPanel
is a simple example of a single component, while JGraph
is a more flexible library that uses the flyweight pattern for efficient rendering.
精彩评论