java - Disposing Graphics2D drawing after it is drawn -


for game i'm working on need draw rectangle gets smaller , smaller. have figured out how draw rectangle smaller using swing timer this:

    timer = new timer(100, new actionlistener(){         public void actionperformed(actionevent e){             graphics2d g2d = (graphics2d) panel.getgraphics();             if(width > 64){                 g2d.drawrect(x,y,width,height);                 x += 1;                 y += 1;                 width -= 1;                 height -= 1;             }         }     });     timer.start(); 

the problem having wont remove rectangle drawn before wont it's shrinking more it's filling in. how remove drawn rectangle right after smaller rectangle have been drawn?

you might start with:-

change:

graphics2d g2d = (graphics2d) panel.getgraphics();  

to:

repaint(); 

the graphics instance getgraphics() transient, window might repainted whenever jvm feels necessary.

the overridden method might this.

    @override     public void paintcomponent(graphics g){         super.paintcomponent(g);  // clears bg         graphics2d g2d = (graphics2d)g;         if(width > 64){             g2d.drawrect(x,y,width,height);             x += 1;             y += 1;             width -= 1;             height -= 1;         }         // toolkit.getdefaulttoolkit().sync();          // g2d.dispose();  no!  don't dispose of graphics instance     } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -