2

So, I'm trying to create a radial gradient (for static lighting in a game). I came up with this:

int step = 4;
int forLen = (radius / step);
g.setColor(new Color(0, 0, 0, intensity));
for (int i = 0; i < forLen; i++)
    g.fillOval(radius - i * step, radius - i * step, i * step * 2, i * step * 2);

But the result i get is this:

enter image description here

I'd like to get a smoother gradient, like:

enter image description here

I tried playing around with the step and the intensity, but i can't get something similar to the gradient above. What would be a good way of doing this?? Thanks in advance!

Burkhard
  • 14,596
  • 22
  • 87
  • 108
Sylar
  • 357
  • 1
  • 5
  • 15

1 Answers1

2

I don't have a concrete answer, but the look of the second picture left me the impression that the intensity is not decreasing linearly, but rather exponentially.

Could you try instead of doing a linear decreasing function, use a exponential function instead? For every step outwards, multiply the intensity with a fixed number a < 1.

zw324
  • 26,764
  • 16
  • 85
  • 118
  • Maybe not exactly exponentially, but most likely superlinearly. – G. Bach Jun 19 '13 at 15:04
  • 1
    Thanks! ...i took your answer and decide to use it, but in the opposite way. I started from the outer circle, going inwards. With a really low starting intensity (0.01 for example) i multiplied it with a fixed number > 1 (1.02 for example), and that gave me the exact result i wanted, and a lot more control! – Sylar Jun 19 '13 at 15:34
  • @Sylar Great I could help, the way you are using is quite interesting:) – zw324 Jun 19 '13 at 15:40