java – int无法解除引用

我开始在 java(我正在学习microedition),我得到了这个错误:“int无法取消引用”在下面的类中:
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());
    }
}

在这做错了什么?
好吧,我来自PHP和ECMAScripts,我能够以这种方式传递我的函数参数,所以我真的不明白这个错误.

解决方法

drawString中的g是您传入的颜色值,而不是Graphics引用.因此,错误是当您尝试在int上调用方法时,您无法执行此操作.
//            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);
}

相关文章

应用场景 C端用户提交工单、工单创建完成之后、会发布一条工...
线程类,设置有一个公共资源 package cn.org.chris.concurre...
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量