cdarwin
cdarwin

Reputation: 4291

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:

  1. draw the entire graph as a component
  2. 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 clicked point. It seems to me that this is not much efficient. Is the second approach feasible? And which one should be followed, and why?

Upvotes: 5

Views: 3273

Answers (2)

trashgod
trashgod

Reputation: 205785

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.

Upvotes: 2

Frodo Baggins
Frodo Baggins

Reputation: 8543

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.

Upvotes: 7

Related Questions