int cannot be dereferenced

后端 未结 3 856
谎友^
谎友^ 2020-11-30 15:19

I am beginning in java (I\'m learning in microedition) and I got this error: \"int cannot be dereferenced\" in the following class:

class DCanvas extends Can         


        
3条回答
  •  伪装坚强ぢ
    2020-11-30 15:42

    While g is in the paint-method an object of the class Graphics (that contains methods named setColor, fillRect and also drawString) in the method drawString is g defined as an Integer that conatins the value for the color green. Especially in the line g.setColor(r, g, b); you use g to set a color on it and also as the argument for setting the color. int has no method setColor (that also doesn't make sense), so you get an error. You probably want to get an Graphics-object also in this method. As you extend canvas, you can get a graphics-object by calling getGraphics(), so your example could look like this:

    public void drawString(String str, int x, int y, int r, int g, int b){
        getGraphics().setColor(r, g, b);
        getGraphics().drawString(str, x, y, 0);
    }
    

提交回复
热议问题