saplingPro
saplingPro

Reputation: 21329

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

My class named UI extends RoundButton which is as 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 the game is not complete or has just 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 is a method named disableAllButtons which is as follows :

public void disableAllButtons() {
    int count =0 ;
    while(count <= buttons.length-1) {
        buttons[count].setEnabled(false);
        buttons[count].paintComponent(Graphics g); // GENERATES AN ERROR
        // where buttons[count] = new RoundButton()
        count++;  
    }
}

From this method I try to call,paintComponent I overrode in RoundButton class. But I get an error :

')' expected
';' expected
 not a statement
 cannot find symbol
 symbol: variable Graphics
 location: class UI

when I import java.awt.Graphics class.

Why is that ?

Upvotes: 1

Views: 120

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Look at the call buttons[count].paintComponent(Graphics g)...

First of all, you should NEVER call paintComponent yourself, you should let the RepaintManager deal with it. Use repaint instead.

Secondly, Graphics g is not a valid parameter, it's a deceleration.

Check out

For details about Swing and painting.

Also...calling this.setBorder(null); within you paint method is a really, really bad idea. This will trigger a new repaint request to be posted on the Event Dispatching Thread over and over and over and over ... you get the idea. It will consume you CPU

Upvotes: 1

Related Questions