question: private void recalculatPreferredSize ( ) { int maxX = 0 ; int maxY = 0 ; for ( GraphicalNode gn : myGraphicalNodes ) { int x = gn . x + NODE_WIDTH ; int y = gn . y + NODE_HEIGHT ; maxX = Math . max ( maxX , x ) ; maxY = Math . max ( maxY , y ) ; } setPreferredSize ( new Dimension ( maxX , maxY ) ) ; myMaxX = maxX ; myMaxY = maxY ; }
target:  Recalculate preferred size so that graphics fit with nodes positions. 