java - Zooming in and zooming out within a panel -
i have panel in 2d objects moving about. have overridden paintcomponent() necessary. want able zoom in , zoom out area. when zooming in, scrollbars appear 1 can view entire field scrolling. while zooming in , out, 2d objects should increase or decrease in size accordingly. swing component or rather combination of components achieve this?
easiest way modify panel , introduce double indicating zoom level. double indicate scale, 1 normal , higher zoomed in. can use double graphics2d
in paintcomponent
.
such as:
graphics2d g2 = (graphics2d) g; int w = // real width of canvas int h = // real height of canvas // translate used make sure scale centered g2.translate(w/2, h/2); g2.scale(scale, scale); g2.translate(-w/2, -h/2);
for scrolling, put panel in jscrollpane , combine getpreferredsize uses zoom scale. jscrollpane uses preferred size of component put in it. show scrollbars if preferred size exceeds own size.
if change preferred size of panel width , height returns scaled should fine. can return like:
return new dimension(w * scale, h * scale)
Comments
Post a Comment