• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing into jpanel distorts while scrollbars get activated  RSS feed

 
Ahsan Habib
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to design call flow builder. States are nodes,links connect them. I subclass JPanel which is main drawing area. I sub-class JLabel to create nodes and they can be dragged over drawing panel. Node overrides paintComponent and use drawing panel graphics object to draw corresponding link. Whenever one node is dragged, it draws it is neighbour links. When node is dragged beyond view area, scroll pane gets activated. My problem also occurs at this point. Whenever node is dragged beyond display region and scroll bar appears, link gets distorted and they no more connect nodes exactly. When nodes dragged back to normal view area, they are fine again.

If somebody could catch my problem from this scenario, please suggest what should be done. If required I can post the logic.

Thanks in advance.
 
Rob Spoor
Sheriff
Posts: 20892
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done something similar but do not face your problem. My links are just fine.
Can you show us your drawing code?
 
Ahsan Habib
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) DrawingPanel class; Sub-class of JPanel for drawing (JLabels as node and line as links). Nodes are added and linked dynamically.


2) DrawingPanelMouseListener subclasses MouseInputAdapter.


3) Node class extends JLabel. It maintains list of adjacent neighbors to draw line between nodes along with utility data elements.


4) MainPanel extends JPanel. It adds JScrollPanel(drawingPanel) in border layout center and one toolbar is added at north.


5) AppFrame is main class which display GUI


Problem: Inside un-scrolled area, every thing works fine. New nodes created, line is drawn between two nodes, when node dragged corresponding link also redrawn accurately. But when one node taken beyond display area and left/right scroller appears, then link shifts from both src and dst node. It seems calculation for line end points disturbed with new scrolled area. The far I drag that node, line shifts from src(or dest) node proportionately. When that node taken back to normal display area, link connect both nodes in proper point. Might be some viewport issue.

Hope you could figure out my design. Please let me know if further code need to be shown.
[ January 01, 2009: Message edited by: Rob Prime ]
 
Ahsan Habib
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was solved! I was using drawing panle's graphics object to drawlines but using Graphics, what i shud do is to cast it to Graphics2D. I guess parent class Graphics doesn't do coordinate transformations. Thats why when nodes go beyond normal view and scroll enables, lines get shifted when drawing lines using Graphics. Use of Graphics2D takes care of this transformation.

Thanks all.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using AffineTransforms? If so, don't use the Graphics object you pass to the paintComponent method. Use its method which makes a copy object (I think it's create()) and cast to Graphics2D then apply the AffineTransforms to that object. You can get transforms interfering with subsequent transforms and never quite get back to the original position.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!