How can I pan and scale JComponents?

I am trying to create a GUI for graphical programming. I would like to use JUNG to handle the graphical aspects of a drawing. Instead of drawing all of my widgets with Graphics2D, I would like to use Swing widgets as often as possible to take advantage of the Swing event architecture.

I would like to be able to pan, zoom, rotate the canvas the graph is drawn on, while still being able to manipulate the Swing widgets if they are large enough to see them. I also want the widgets to be auto-laid out based on the JUNG layout algorithm I have chosen.

How to combine Graphics2D drawings and Swing components?

I understand that JComponents take care of painting themselves using their methods paint()

. I am having trouble understanding how to create Swing objects that can be affected by Graphics2D transformations and still retain all of their event handling. I can't just show the image of the component. The component must be in real time.

I tried to subclass the widget and add an AffineTransform to the artwork that is passed to the parent method paint

. It rotates the widget, but clipping seems to be disabled.

Any suggestions? For now, I would like to avoid adding another library if I can.

+3


source to share


3 answers


Based on my research, using Piccolo2D provides the ability to pan and scale Swing elements. By using the JUNG graph layout algorithms to lay out the graph and PSVI Piccolo2D nodes, I can generate a graph of Swing components that can be used, decomposed and scaled and panned.



+1


source


JXLayer implemented a project to transform the entire user interface at different scales.



You can check this demo

+2


source


As an example JDigit extends JButton

and overrides paintComponent()

to scale its size Container

. For speed, the component uses pre-rendered glyphs, but deriveFont()

only slightly slower. Similar results can be obtained by implementing an interface Icon

. This example scales the grid of buttons to a few predefined sizes, which does away with the components entirely, scaling the graphics context and all of the content.

+2


source







All Articles