Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better swing gui design?

I am working on a project for an online class I made the mistake of taking this summer and I need to build a gui to show how the huffman code algorithm works. The algorithm part is easy, its not very complicated. However im unsure what the best way to draw the tree(forrest) at each step. It would have to start out as just n nodes (with chars in them) on the screen and then you would press a "next" button and it would pick the two lowest nodes weighted (based on character frequency) characters and make them children of a new node (with just a weight - no char) and then update the screen/panel.

I have made swing gui before, my skills are nothing special but I know my way around. However im stuck on this implementation. I have a couple hundred lines of code written right now, but it doesnt work and I think its bad anyway, so I want to "start over" and plan it out better. So Id just like some advice on the data structure to keep track of the nodes and how to draw them on the screen.

I was using an ArrayList of JPanels as nodes and trying to draw them to a null layout. Im sure this is awful and id like to know a better way. Possibly GridBagLayout?

NOTE: don't say JTree.

like image 699
Bill Avatar asked Jan 19 '26 20:01

Bill


2 Answers

A good option is to just use a library for drawing trees/graphs. I've had good success with Visual Library in the past.

Another possibility is Prefuse

like image 55
Jay Askren Avatar answered Jan 21 '26 09:01

Jay Askren


Instead of wrestling with the different Swing layouts you could just do custom 2D drawing. See for a simple enter link description hereexample here on how to get started.

like image 20
Paul Sasik Avatar answered Jan 21 '26 09:01

Paul Sasik