The code is below. Basically, I have an anonymous instance of a “Cell” class which is supposed to draw something to the screen. Within one of the methods that I’ve overridden (annotated), I call a method that exists only within the anonymous class (randomFireBubble, marked with comments). Here’s the issue: the method executes and runs (I’ve put in various print statements to test this), but it doesn’t draw anything to the screen! But that’s not even the problem. The problem is that if I replace the call to the method with the body of the method, it runs perfectly as intended.
In essence my problem is this: if I replace a call to a method with the body of the method, and everything runs fine, why can’t I just call the method? This is baffling me.
Edit: I should clarify that it doesn’t really matter what randomFireBubble does. The issue is that if I have the body of the method in place of its call, then it works, but if I call the method, then it doesn’t.
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;
}
}
};
}
That’s because “x” and “y” are defined within “draw” but not within “randomFireBubble”. I don’t even know how this compiles for you. It probably doesn’t and you’ve missed it, or there’s a problem with the code you’ve pasted above.