Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes how to draw lines across components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to draw lines across components" Watch "how to draw lines across components" New topic
Author

how to draw lines across components

Korol Bloom
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Hi,
I have a jframe, that contains a Menubar and a jscrollpane. The Jscrollpane shows a jlayerdpane containing ca 100 components. Some of them should be connected with a "cable", that will be represented by a line. So I have to draw lines across the components. The extreme case is, that the upper left component of the jlayerdPane should be connected to the lower right one -- I have to draw all across.
I have tried 2 ways to solve the problem, but still have trouble with both them.
1. Add a jpanel to the top-Layer of the jlayeredPane and write a paint method for it, that draws a line from one corner to another. So I just need to set the jpanels bounds that they fit to the 2 components to connect. The components behind it stay visible, because the jpanel is not opaque.
Problem: If I zoom the layeredpane so that the whole viewport of my scrollpane is covered by the jpanel on which i draw the line, then the components below won't be painted any more.
2. Draw the lines on the glass-pane. In this case I compute the coordinates of the two components to connect relative to the glasspane.
Problem: Maybe one of the components is not shown (scrolled away), so its coordinates might be negative. I want to draw the cable anyway, with the result, that the cable is painted across my menubar, because glasspane covers menubar, too.
Any suggestions? Which way is preferrable, how to fix the problems? Is there another solution?
, korol
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The thing that I would do would be override the paintComponent() method of the container that contains all the components you want to connect. It's easy for the container itself to get the coordinates of all of it's children, and the container itself is inside the scroll pane, too.

The only thing that this approach ignores (but also the solutions you mentioned) is that this is a JLayeredPane, and the components might be on different layers. You *could* do this by drawing the lines in segments across the layers they need to pass through, but I can't think of a way to do this between the painting of the components on different layers. If you don't need layers, then you just draw the lines in paintComponent() and you don't have this problem.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Korol Bloom
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Right, that might be the easiest solution. In fact I don't need the layeredpane any more, because I needed the layer just to draw lines above components. I think it is necessary to swap the order of calling paintChildren() and paintComponent(), right? My jpanels (ex-layeredpane) paint method now looks like this:

public void paint(Graphics g) {
myPaintBackground(g); // paints background color
paintChildren(g);
myPaintComponent(g); // paints lines
}

Thanks, Nathan!
, korol
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Yes, something like this should work. If you run into any issues with re-ordering paintChildren() and paintComponent(), you could also just add a new method, like paintLines( Graphics g ) and then override paint( Graphics g ) to call super.paint(g) and then drawLines(g).
 
jQuery in Action, 2nd edition
 
subject: how to draw lines across components
 
Similar Threads
How to reset jpanel...
HELP ! Best Architecture for Swing Paint Tools
Paint Drag Preview
I need a little guidance
constructing a component (beginner)