java - JButtons acting up in paintComponent() -


i sorry non-descriptive title not sure how communicate problems. starters jbuttons every , again creating multiple times in same order loop should create them. issue having when reposition them using setlocation() method creates new jbuttons want them leaves old ones are. don't know if have refresh graphics or going on. help.

the array playerhand()is defined in player class , 5 in length.

public void paintcomponent(java.awt.graphics g){     setbackground(color.green);      // create graphics2d object graphics object drawing shape     graphics2d gr=(graphics2d) g;      for(int x=0;x<player.hand.size();x++){         card c = player.hand.get(x);                    //c = current element in array         c.xcenter = 30 + 140*x;                                  c.xcord = c.xcenter - 30;         c.ycord = 0;           //5 pixel thick pen         gr.setstroke(new java.awt.basicstroke(3));                     gr.setcolor(color.black);                       //sets pen color black         gr.drawrect(c.xcord, c.ycord, c.cardwidth-1, c.cardheight-1);          //draws card outline         gr.setfont(new font("serif", font.plain, 18));          gr.drawstring(c.name, c.xcord+10, c.ycord+20);          gr.drawstring("atk: ", c.xcord+10, c.ycord+60);         gr.drawstring(""+c.attack, c.xcord+60, c.ycord+60);          gr.drawstring("def: ", c.xcord+10, c.ycord+80);         gr.drawstring(""+c.defence, c.xcord+60, c.ycord+80);          gr.drawstring("hp: ", c.xcord+10, c.ycord+100);         gr.drawstring(""+c.health, c.xcord+60, c.ycord+100);          jbutton button = new jbutton(c.name);         button.setsize(c.cardwidth, c.cardheight);         //button.setlocation(c.xcord, c.ycord);         this.add(button);         repaint();     } }   //end of paintcomponent 

there several problems (marked "!!!!" comments) in method below:

public void paintcomponent(java.awt.graphics g){     setbackground(color.green); // !!!!     graphics2d gr=(graphics2d) g;     for(int x=0;x<player.hand.size();x++){          // .... etc ....          jbutton button = new jbutton(c.name);  // !!!! yikes !!!!         button.setsize(c.cardwidth, c.cardheight);         //button.setlocation(c.xcord, c.ycord);         this.add(button);  // !!!! yikes !!!!         repaint();  // !!!!     } } 
  • do not add components gui's in paintcomponent(...). never this. ever.
  • this method should drawing , drawing , never program logic or gui building.
  • you not have full control on when or if called.
  • it needs fast possible not make program poorly responsive.
  • avoid object creation within paintcomponent(...) if possible.
  • avoid file i/o in method (though not problem code above).
  • don't call setbackground(...) in method. in constructor or other method.
  • never call repaint() within method.
  • don't forget call super's paintcomponent(g) method in override.

to go on problems:

for starters jbuttons every , again creating multiple times in same order loop should create them.

this because not have control on when or how paintcomponent called. jvm may call in response information operating system dirty region, or program may request repaint, reason, program logic , gui building should never in method.

another issue having when reposition them using setlocation() method creates new jbuttons want them leaves old ones are.

quite you're seeing image remnants not calling super's paintcomponent method. not doing this, don't have jpanel repaint , remove old image pixels need replaced or refreshed.

i don't know if have refresh graphics or going on.

no, need change everything.

it looks have re-think gui's program flow , logic , re-write bit of code.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

node.js - Node - Passport Auth - Authed Post Route hangs on form submission -

Does Firefox offer AppleScript support to get URL of windows? -