我遇到的最奇怪的错误

时间:2012-02-10 02:38:06

标签: java graphics methods anonymous-class

代码如下。基本上,我有一个“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;
            }
        }
    };

}

1 个答案:

答案 0 :(得分:1)

那是因为“x”和“y”在“draw”中定义但不在“randomFireBubble”中定义。我甚至不知道这是如何为你编译的。它可能没有,你错过了它,或者你上面粘贴的代码有问题。

相关问题