代码如下。基本上,我有一个“Cell”类的匿名实例,它应该在屏幕上绘制一些东西。在我重写(带注释)的方法之一中,我调用了仅存在于匿名类(randomFireBubble,用注释标记)中的方法。问题是:该方法执行并运行(我已放入各种打印语句来测试这一点),但它不会在屏幕上绘制任何内容!但这还不是问题。问题是,如果我用方法的主体替换对该方法的调用,它会按预期完美运行。
本质上我的问题是这样的:如果我用方法的主体替换对方法的调用,并且一切都运行良好,为什么我不能只调用该方法?这让我很困惑。
编辑:我应该澄清一下,randomFireBubble 的作用并不重要。问题是,如果我用方法的主体代替它的调用,那么它就可以工作,但是如果我调用该方法,那么它就不行。
public static Cell Fire (int x, int y, Grid grid)
{
return new Cell(x, y, grid)
{
@Override
public void draw (int size, int x, int y, Graphics graphics)
{
super.draw(size, x, y, graphics);
cellColor = fireColor(Math.random());
randomFireBubble(graphics, size); // < -- Problem here
}
private void randomFireBubble (Graphics graphics, int size)
{
int x1 = (int) StaticCalculations.randomDoubleBetween(- x * 1.5, x * 1.5);
int y1 = (int) StaticCalculations.randomDoubleBetween(- y * 1.5, y * 1.5);
int width1 = (int) StaticCalculations.randomDoubleBetween(0, size * 1.5);
graphics.setColor(fireColor(Math.random()));
graphics.fillOval((x + 1) * size - size / 2 + x1, (y + 1) * size - size / 2 + y1, width1, width1);
}
//This method is irrelevant, it works perfectly fine.
private Color fireColor (double r)
{
if(r <= 0.33)
{
return Color.yellow;
}
else if(r <= 0.66)
{
return Color.orange;
}
else
{
return Color.red;
}
}
};
}
最佳答案
这是因为“x”和“y”是在“draw”中定义的,而不是在“randomFireBubble”中定义的。我什至不知道这对你来说是如何编译的。它可能没有,并且您错过了它,或者您上面粘贴的代码有问题。
关于java - 我遇到过的最奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9221995/