Java – int cannot dereference

I started in Java (I was learning microedition), and I got this error: "int cannot dereference" in the following class:

class DCanvas extends Canvas{
    public DCanvas(){

    }

    public void drawString(String str,int x,int y,int r,int g,int b){
        g.setColor(r,g,b); //The error is here
        g.drawString(str,x,y,0); //and here
    }

    public void paint(Graphics g){
        g.setColor(100,100,220);
        g.fillRect(0,getWidth(),getHeight());
    }
}

What did I do wrong here? Well, I'm from PHP and ecmascripts. I can pass my function parameters in this way, so I really don't understand this error

Solution

The G in drawstring is the color value you passed in, not the graphics reference Therefore, the error is that when you try to call a method on int, you cannot perform this operation

//            Passing an integer 'g' into the function here |
//                                                          V
public void drawString(String str,int b){
//  | This 'g' is the integer you passed in
//  V
    g.setColor(r,b);
    g.drawString(str,0);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>