1

I have an application, in which I want to displaying a number of image components. Each item of this will be a custom jcomponent which will has the image in a BufferedImage object to draw it in paintcomponents(Graphics g) overrided method. I will also use a JPanel for the grid with a gridlayout or flowlayout for place the custom image jcomponents, also the grid's layout will be inside an JscrollPane.

My question is what happens, when the number of the images I must to put into the grid will become big. With use of the jscrollpane, the number of the components so the number of the images must be draw will be smaller like 20-30 components, but each component inserted into the grid will have an object of BufferedImage to keep the corresponding image. This is bad for the perfomance and the memory consuption? May can use some pattern? for example if I use a main point of reference to load the images from disk to bufferedimage and depending of which rectangle of the scrollpane the user then release some bufferedimage where they putted away in the grid from the current position of the jscrollpane?

Thank you.

javment
  • 368
  • 1
  • 5
  • 17
  • Thank you, you mean, use a JTable with a `Custom cell renderer` to display my custom `JComponent` inside cells, because the `JTable` will call the renderer only when the specific cell is in the visible area of the scrollpane so no waste of memory, of load all the images.But the already load new images from the disk, isn't a problem? – javment Aug 04 '12 at 08:39

2 Answers2

2

You're going to have to prototype the essential variables and profile the results. Then compare those results to the capabilities available on your lowest common denominator target platform. An sscce is invaluable. This one, for example, will let you easily vary N, the number of images.

Some view alternatives are mentioned here.

Noted in comment: To conserve memory, consider an LRU cache.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you, also you give me an example code with the use of `JTable` you agree with my comment above to @max – javment Aug 04 '12 at 08:41
  • 1
    I'd expect `JTable` rendering to scale better, but you'll have to profile your prototype. To conserve memory, consider an [LRU cache](http://stackoverflow.com/q/221525/230513). Don't neglect to up-vote answers you found helpful. – trashgod Aug 04 '12 at 09:43
  • Thank you,I will see how to use the `jtable` into my app and also see the LRU cache post you give me. Sorry for neglect to up-vote! – javment Aug 04 '12 at 09:51
  • Thank you, an LRU Cache is what I wanted! – javment Aug 10 '12 at 13:45
2
  1. Thread the loading of the images, so that the UI isn't delayed by the loading
  2. Scale the images, probably to fit the size of the panel, no point in wasting memory
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hi, for the 1st ok, of course I make the loading into a seperate thread. But for the 2nd I want the image thumbnails has a default size proper for viewing by the user extra images will be view with the use of the scrollpane – javment Aug 04 '12 at 08:37
  • @trashgod Thank you, I 'll see your example you post me! – javment Aug 04 '12 at 09:52
  • @javment the basic idea is, if you don't need to display the images at there full resolution (all time), it's better to scale the image when you load it, rather then waste memory (& time trying to scale the image in real time) – MadProgrammer Aug 04 '12 at 09:55
  • @MadProgrammer you have right, for this reason I will already create thumbnails of the images, in the specific resolution I want to display and these thumbs are saved to disk.More specific, I make a quick scan of the folder, find the images, create the corresponding thumbnail for each one and save it into to a specific folder and after I will display. – javment Aug 04 '12 at 10:11