java - Why am I getting an error as I try to call paintComponent? -


my class named ui extends roundbutton follows :

public class roundbutton extends jbutton {      public roundbutton(string label) {         super(label);         this.setcontentareafilled(false);         dimension size = this.getpreferredsize();         size.height = size.width = math.max(size.height, size.width);         this.setpreferredsize(size);     }      @override     protected void paintcomponent(graphics g) {         if(!gamestate.getifcomplete()) { // if game not complete or has started             this.setborder(null);             g.setcolor(color.black);             g.fillrect(0, 0, this.getsize().width, this.getsize().height);             if(this.getmodel().isarmed()) {                g.setcolor(color.red);             }else {                 g.setcolor(color.green);             }             g.filloval(0,0,this.getsize().width-1,this.getsize().height-1);             super.paintcomponent(g);         }else {             this.setborder(null);             g.setcolor(color.black);             g.fillrect(0, 0, this.getsize().width, this.getsize().height);             if(this.getmodel().isarmed()) {                g.setcolor(color.blue);             }else {                 g.setcolor(color.blue);             }             g.filloval(0,0,this.getsize().width-1,this.getsize().height-1);             super.paintcomponent(g);          }     } } 

inside ui there method named disableallbuttons follows :

public void disableallbuttons() {     int count =0 ;     while(count <= buttons.length-1) {         buttons[count].setenabled(false);         buttons[count].paintcomponent(graphics g); // generates error         // buttons[count] = new roundbutton()         count++;       } } 

from method try call,paintcomponent overrode in roundbutton class. error :

')' expected ';' expected  not statement  cannot find symbol  symbol: variable graphics  location: class ui 

when import java.awt.graphics class.

why ?

look @ call buttons[count].paintcomponent(graphics g)...

first of all, should never call paintcomponent yourself, should let repaintmanager deal it. use repaint instead.

secondly, graphics g not valid parameter, it's deceleration.

check out

for details swing , painting.

also...calling this.setborder(null); within paint method really, bad idea. trigger new repaint request posted on event dispatching thread on , on , on , on ... idea. consume cpu


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -