我开始在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, 0, getWidth(), getHeight());
}
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?好吧,我来自PHP和ECMAScripts,我能够以这种方式传递我的函数参数,所以我真的不明白这个错误.
该g在drawString为你传递的颜色值,而不是你Graphics参考.因此,错误是当您尝试在某个方法上调用方法时int,您无法做到.
// Passing an integer 'g' into the function here |
// V
public void drawString(String str, int x, int y, int r, int g, int b){
// | This 'g' is the integer you passed in
// V
g.setColor(r, g, b);
g.drawString(str, x, y, 0);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10248 次 |
| 最近记录: |