This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.
Joined: Jan 14, 2004
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.
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?