java.lang.IllegalArgumentException thrown in java.awt.Color constructorWhen running an applet in a browser using the Sun JRE, an
IllegalArgumentExceptionis thrown in thejava.awt.Colorconstructor. The same applet runs under the Microsoft VM.
This exception is caused by passing over-bound or under-bound values to the
Colorconstructor in the Sun JRE.The Java class libraries in the Sun JRE have changed over time. Some APIs have been clarified, some have been deprecated, and some have had their implementation altered.
The result of passing over-bound/under-bound values in the
Colorconstructor was not well defined, and the Microsoft VM would automatically readjust the values to maximum color values and minimum color values automatically. The Sun JRE took a different approach, using anIllegalArgumentExceptionto let the programmer know that they specified an out-of-bounds value.
Code defensively to ensure only valid color values are passed to the
Colorconstructor. For example,void Color newColor(int r, int g, int b) { return new Color(r, g, b); }The code should be changed to
int ensureColorRange(int v)
{
if (v < 0)
return 0;
else if (v > 255)
return 255;
else
return v;
}
void Color newColor(int r, int g, int b)
{
r = ensureColorRange(r);
g = ensureColorRange(g);
b = ensureColorRange(b);
return new Color(r, g, b);
}
N/A