Monday, May 9, 2011



Java error "Color parameter outside of expected range: Red Green Blue" can be encountered when we use the java.awt.Color class with wrong arguments for RGB (Red Green Blue).

See the wrong code below:
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Color;
public class MyFrame extends JFrame
{
public MyFrame()
{
Container container = getContentPane();
container.setBackground(new Color(-5,256,300)); // wrong value for RGB
setSize(800,600);
}
public static void main(String args[])
{
MyFrame myFrame = new MyFrame();
myFrame.setVisible(true);
}
}



See that, we have used -5, 256, 300 for Red, Green and Blue values respectively in the java.awt.Color class constructor. These arguments are illegal as the supported values are in the range 0 to 255.

So a correct code is:

container.setBackground(new Color(5,156,200)); // RGB values are in the range 0 to 255

If the constructor with float arguments is used than the correct RGB values are in the range 0.0 to 1.0

For more details http://download.oracle.com/javase/1.5.0/docs/api/java/awt/Color.html


If the above solutions do not work for you. Please comment below giving your code. I'll try to respond.

0 comments:

Post a Comment