java - Dealing with memory making Swing animations -


it's first time writting there after reading , searching lot subject i'm going explain, didn't found useful me (i've checked post of guy explaining similar iphone app, without final solution). sorry english i'll try best hehe :).

ok, i'm starting: have java application shows set of animations @ same time on jpanel. these animations java objects animation (inheritates jlabel), class of own that stores array of imageicon (representing frames of animation) , integer pointer, showing actual frame paint (index). class has overriden method setpaint() in way:

@override public void paintcomponent(graphics g) {         g.drawimage(getimageat(getcurrentindex()).getimage(), getx2(), gety2(), getw(), geth(), null);         //getimage @ retrieves frame @ "index" position frame array.         if (getcurrentindex() < gettotal() - 1) {             incrementindex();//increments frame 1 (next frame)         } else {             resetindex();//set index 0 (frame 0)         } } 

on other hand, have display, inheritates jpanel , responsible repainting panel (it uses javax.swing.timer object , repaints every 50 ms).

all images every animation object have same size 768x576 , png format transparencies (thus can paint many animations @ time).

the problem when load animation occupies memory because creates don't know why either how 200 mb of "int" objects apart of 2mb occupies. when load more animations heap's space of jvm rockets (up 1.7 gb... didn't want try anymore xd). example, 6 animations may 405 frames (90 in average). i've collected data jprofiler, can't post capture get: memory usage of app jprofiler:

  • java.awtrectangle 3351 kb

  • sun.java2d.sungraphics2d 16.510 kb (i think size of animations , sounds logical).

  • java.lang.string 669kb

  • int[] 1337 mb ...


in display, loading images process in way:

add(new animation (loadimageset(constants.dir_cinta_eje,"all"),             constants.a_cinta_eje, 75,-200,768,576));//1 

where dir_cinta_eje directory frames of animation , value "all" means want load frames.

there way avoid memory used grows linearly number of animations? why ​​"int" values occupies much? pixels of each image?

and yes, have increased heap space 2gb , it's working, @ high cost. thank can provide. love forum, really.

regards.

i've been working hard looking satisfactory solution , i've got it! that's have done:

class animation no more inheritates jlabel implements actionlistener, in actionperformed() method increment frame show each 1 (i use timer class). therefore, animation no more override paintcomponent() method (it's work displaypanel).

in displaypanel, use thre global variables:

private bufferedimage _auxiliary_image; private imageicon _image2display; private arraylist<animation>;//i store there running animations there 

and now, override there displaypanel paintcomponent(), thus:

 @override     public void paintcomponent(graphics g){         super.paintcomponent(g);         (int = 0; < _animations.size(); i++) {             if(i==0){                 _auxiliary_image.creategraphics().drawimage(_animations.get(i).getactual().getimage(), 0, 0, color.white, null);             }else{                 _auxiliary_image.creategraphics().drawimage(_animations.get(i).getactual().getimage(), 0, 0, null);             }          }         _image2display.setimage(_auxiliary_image);         _image2display.painticon(this, g, 50, -200);     } 

the reason why difference 2 cases, 1 i==0 , rest of frames on painting saw black background , whole positions of animation, way solve issue.

now, 8 animations 50 frames each 1 occuppies 360 mb of memory instead 1.3 gb last time =) (i paint 1 image, results merging frames printed @ same time each animation).

thank , hope help!!!


Comments