java - Does g.drawImage() render only the part of the image visible on a JPanel, or does it "keep in mind" the rest of the image? -


let's have following code:

(in class extends jpanel):

public void paintcomponent(graphics g) {     g.drawimage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null); } 

if dx1 , dy1 negative or dx2 , dy2 larger width of jpanel (in other words, part of image off-screen), drawimage() adjust rendered "pays attention to" part visible on jpanel? curious because if draw large image on jpanel, paintcomponent() slow.

there 2 sides of problem here...

1. loading large images

you have large image loaded memory in code example (you painting directly onto component). means if have big image - encounter first problems when start loading memory paint (doesn't matter how) - consume a lot of memory when loaded.

this should main headache when work large images standard java doesn't provide lot of tools. may load full image using basic tools.

you might want alternative ways of loading part of image (not sure possible) or splitting large image parts , loading displayed image parts. 3rd party libraries jai might that.

anyway, "lyrics" - let's go second problem asked about.

2. painting large image onto component

i bet have read swing tutorials , might aware of clip. might know set component's current visible bounds. yep, visible part.

so if have panel 5000x5000 px size 5000x5000 px image painted on it, visible part of panel 500x500 px - image clipped underlying graphics , part fits clip painted.

this optimization works various shapes painting/filling , other operations graphics. not obvious thing in cases better paint full shape/image , let underlying graphics optimize painting operation. in cases times faster clip shape/image manually , paint result.

one more thing - drawing image onto graphics 1 of fastest operations in graphics2d wouldn't focus on it.

you can check small example indicates painting optimizations provided graphics2d internal implementations:

public class imagedrawtest {     public static void main ( string[] args )     {             final imageicon icon = new imageicon ( "c:\\large.jpg" );          jcomponent c = new jcomponent ()         {             protected void paintcomponent ( graphics g )             {                 super.paintcomponent ( g );                  long time = system.nanotime ();                 g.drawimage ( icon.getimage (), 0, 0, null );                 time = system.nanotime () - time;                 system.out.println ( time );             }         };          jframe f = new jframe ();         f.getcontentpane ().setlayout ( new borderlayout () );         f.getcontentpane ().add ( c );         f.setsize ( 200, 100 );         f.setlocationrelativeto ( null );         f.setdefaultcloseoperation ( jframe.exit_on_close );         f.setvisible ( true );     } } 

instead of c:\\large.jpg use available large image path.

simply run example , resize frame change component's visible area see output. display painting time in nanoseconds each repaint - vary lot depending on visible area size.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -