• 0

[Java]Only get one colour with a BufferedImage


Question

I'm having trouble trying to get a colour other than blue with a BufferedImage

So I've got my buffered image, and just for testing I've got a loop to colour in the top left corner, however no matter what RGB value i set, it only colours in blue. Changing the value changes how bright the blue is.

static BufferedImage I = new BufferedImage(X, Y, BufferedImage.TYPE_INT_RGB);

...

for (int i = 0; i<100; i++)
		{
			for (int j = 0; j < 100; j++)
			{
				I.setRGB(i,j,100);
			}
		}
		Frame f = new Frame("Mandelbrot");
		f.add("Center", new MandelCanvas());
		f.setSize(X,Y);
		f.setVisible(true);

...

class MandelCanvas extends Canvas
{
   public void paint(Graphics g)
   {
	   g.drawImage(Mandelbrot.I, 0, 0, null);
   }
}

Is there some special voodoo magic I'm missing?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Well, the setRGB(int x, int y, int RGB) means you need to use the full decimal value for RGB. So 100 is equivalent to (r=0,g=0,b=100).

Color codes are numbers, so that if converted to hex, the last 2 digits are blue, the middle 2 green, and the first 2 red. So your RGB value of 100:

Hex for 100 : 0x000064 <=only a blue component

If you want white for instance: white has the hex 0xFFFFFF which is 16777215 in decimal

Java has the Color class which makes this easier to do. Try...

I.setRGB(i,j,new Color(255,255,255).getRGB());

You make a new color then convert it to its decimal form.

See: http://java.sun.com/javase/6/docs/api/java/awt/Color.html

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.