I am am trying to build a user interface for graphical programming. I would like to use JUNG to handle the graph aspects of the drawing. Rather than draw all of my widgets using Graphics2D, I would like to use Swing widgets as often as possible to take advantage of Swing's event architecture.
I would like to be able to pan, zoom, rotate the canvas the graph is drawn on, but still retain the ability to manipulate the Swing widgets as long as they are large enough to see. I also want the widgets to be automatically laid out based on the JUNG layout algorithm I have chosen.
How do I combine Graphics2D drawings and Swing components?
I understand that JComponents take care of drawing themselves using their paint()
methods. What I am having trouble understanding is how to create Swing objects that can be affected by Graphics2D transforms and still retain all of their event handling. I can't just show an image of the component. The component must be live.
I've tried subclassing a widget and adding a AffineTransform to the graphics object that is passed to the parent's paint
method. It rotates the widget, but the clipping appears to be off.
Any suggestions? For the time being, I'd like to avoid adding another library if I can.
Based on my research, using Piccolo2D provides the ability to pan and zoom Swing elements. Using JUNG's graph layout algorithms to layout the graph and Piccolo2D PSwing nodes, I can generate a graph of Swing components that are usable, can be laid out by algorithm, and are zoom and panable.
JXLayer project has implementation to transform the whole UI to different scales.
You may want to check out this demo
As an example, JDigit extends JButton
and overrides paintComponent()
to scale to the size of its enclosing Container
. For speed, the component uses pre-rendered glyphs, but deriveFont()
is only slightly slower. Similar results can be obtained by implementing the Icon
interface. This example scales a grid of buttons to a few, pre-determined sizes, and this example forgoes components entirely, scaling the graphics context and all contents.