Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 267
cdarwin Avatar asked Sep 03 '11 11:09

cdarwin


2 Answers

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.

like image 81
Thomas the Tank Engine Avatar answered Nov 01 '22 05:11

Thomas the Tank Engine


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.

like image 37
trashgod Avatar answered Nov 01 '22 07:11

trashgod