• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Drawing lines between JPanels/JInternalFrames

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!!..you have been a great help
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?.
 
reply
    Bookmark Topic Watch Topic
  • New Topic