public static double selectZoomToShowAll ( JComponent panel , int width , int height ) { int w = panel . getWidth ( ) ; int h = panel . getHeight ( ) ; if ( w == 0 ) { w = panel . getPreferredSize ( ) . width ; h = panel . getPreferredSize ( ) . height ; } double scale = Math . max ( width / ( double ) w , height / ( double ) h ) ; if ( scale > 1.0 ) { return 1.0 / scale ; } else { return 1.0 ; } }
Select a zoom which will allow the entire image to be shown in the panel