aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Drawing lines between JPanels/JInternalFrames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing lines between JPanels/JInternalFrames" Watch "Drawing lines between JPanels/JInternalFrames" New topic
Author

Drawing lines between JPanels/JInternalFrames

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I am slowly working on a proof of concept for a friend of mine and I need to create primative shaped nodes and be able to draw lines between them, drag the nodes around, and have the lines stay connected.

I know about JGraph however, I don't need as much functionality nor is their license going to fit with this project. So I need to implement the base functionality myself.

At first I was looking into custom JPanels but part of the functionality I need already exists in a JInternalFrame, but I don't need the frame decorated and I need to be able to drag using any part of the node. I was able to get a JPanel to drag where I wanted it, but the drawing of lines and specifically keeping them connected while dragging the JPanel is giving me fits. I don't have my code in front of me at the moment so I can't show what I've done.

What I am looking for is some suggestions to solving this. What have you done? Did you use custom JPanels or some other customized component, etc? How about the connection of lines? Thanks for any tips.


GenRocket - Experts at Building Test Data
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I need to create primative shaped nodes
You could draw these on a JPanel.
part of the functionality I need already exists in a JInternalFrame
I wonder what this is.
How about the connection of lines?
This is usually easy enough. For the parent component which contains the node components you loop through the children and draw lines among them.
looking for ... some suggestions
For opaque components or (java.awt.geom) primitives you can draw to the center of the node before rendering the node. For non–opaque components like JLabel, JComponent or primitives you need to do a little more to get the lines to end at the edge of the node. I can demonstrate this if you are interested. Ditto if you want the lines to end at a corner or edge–center of the node depending on relative locations of any two nodes.
You can draw the lines among nodes with every trip through paintComponent or you can keep the lines in an array/list and reset them as needed for node moves.
Here's an idea for using opaque components with the connecting lines joined at the node centers.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks Craig. I am going to mess around with the code you have provided later this evening and then I am sure I'll come back with some questions.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

I allways loved Sun's JDK demo app (comes with the JDK, including source) to learn about linking graphic objects.
demo\applets\GraphLayout\example1.html.

Go ahead, grab one of the objects and drag it around.

Regards, Jan
[ February 25, 2007: Message edited by: Jan Cumps ]

OCUP UML fundamental and ITIL foundation
youtube channel
Marcus Senna
Greenhorn

Joined: Feb 22, 2007
Posts: 21
Hey, I am developing an application which requires the linking of components by lines, but i would like to do this at the click of a button.
I am also using opaque components, so i would like to know if you could help me with regard to positioning the ends of the lines at the edge of the components. Thanks in advance.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
i would like to do this at the click of a button
This would be easy to control with booleans in paintComponent which are set in the listeners of your buttons.
I am also using opaque components
If you are using opaque (filled background) components then the technique shown in LinkedComponents above should work okay.
help me with regard to positioning the ends of the lines at the edge of the components
This is necessary for non–opaque (transparent/translucent background) components and primitives.
Marcus Senna
Greenhorn

Joined: Feb 22, 2007
Posts: 21
Thanks!!!..you have been a great help
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I've been messing around with the code provided by Craig and doing some proof of concept work. It's pretty simple and seems to work nicely. The problem I am running into now is how to make the connecting lines their own component. I tried making them a JPanel on their own but was having issues keeping them connected to the other nodes. So what I have done in the meantime is I am keeping a list of Links. Each link has a target and parent object which are the nodes. in the paintComponent method, instead of drawing lines between every component I simple iterate over the list of links and draw lines from source to target.

While this approach works, it's not very robust. I'd like to more control over the links themselves. Any tips or suggestions?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I'd like to more control over the links themselves.
Here's some exploratory code along these lines.
Looks like someone else has been thinking about this: see reply 3 on the thread How do I "connect" some buttons with the drawing?.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing lines between JPanels/JInternalFrames