java - What is the difference between createCompatibleImage with Transparency.OPAQUE and simple BufferedImage constructor with BufferedImage.TYPE_INT_ARGB? -


what difference between version 1 , version 2? seem same in situation, read everywhere version 1 better approach. why?

public bufferedimage getimage(icon icon) {     int w = icon.geticonwidth();     int h = icon.geticonheight();      // version 1     graphicsdevice gd = graphicsenvironment.getlocalgraphicsenvironment().getdefaultscreendevice();     bufferedimage image = gd.getdefaultconfiguration().createcompatibleimage(w, h, transparency.opaque);      // version 2     // bufferedimage image = new bufferedimage(w, h, bufferedimage.type_int_argb);      graphics2d g = image.creategraphics();     icon.get().painticon(null, g, 0, 0);     g.dispose();      return image; } 

in general, first approach results in image requires less transformations displayed.

in best case possible, "first approach" image have same memory layout actual screen memory layout, meaning in order display image on screen image data can copied is. same true "second approach" image if screen memory layout argb (with 8 bits per component) , in other situations image have (automatically , transparently user code) transformed target format.

the real cases somewhere between , can more involved:

  • the computer can have multiple graphic devices (multiple video cards) different configurations;
  • the user can change graphics configuration while program running;
  • the os can change graphics configuration (think of situation when win7 disables or enables aero).

theoretically, should re-create image each time image format , graphicsconfiguration format become incompatible.

practically, can use new bufferedimage(w, h, bufferedimage.type_int_argb) (or other specific image type suitable needs) until can prove usage of generic bufferedimages causes application run (because of transformation target device format) or consume excessive memory (because transformation requires additional memory).


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -