[java] Colors in Java

Started by
7 comments, last by gpalin 21 years, 4 months ago
How can you use red, green, and blue values to construct a single color? I mean, I''ve seen it before, a long time ago, I don''t remember where. It was where you had, say, values for r, g, and b, and you could call a method or something like that to combine those colors and set it as the current color for Java to use. I hope you understand my question! Grant Palin
Grant Palin
Advertisement
Basically:

Color c = new Color(255,255,255); // rgb values
!
And then I could do:

g.setColor(Color.c);

Right?

Grant Palin
Grant Palin
Actually, you''d be creating a Color object, so it''d be g.setColor(c). Color.name is used for default colors...
Example: Color.BLACK, g.setColor(Color.BLACK).

See, the default colors are static fields of the Color class. =)

This isn''''t life in the fast lane, it''''s life in the oncoming traffic.
-- Terry Pratchett
Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,fasting, good works and so on. Up until Doom, no one seemed to have thought about thedouble-barrel shotgun. Eat leaden death, demon... -- Terry Pratchett
Right, I gotcha. Thanks!!

Grant Palin
Grant Palin
g.setColor(new Color(r,g,b));
Neither of the solutions provided work for me.

M:\Comp132\ColorPicker.java:33: cannot resolve symbol
symbol : constructor Color (int,java.awt.Graphics,int)
location: class java.awt.Color
Color color = new Color(r,g,b);

M:\Comp132\ColorPicker.java:36: cannot resolve symbol
symbol : constructor Color (int,java.awt.Graphics,int)
location: class java.awt.Color
g.setColor(new Color(r,g,b));

Grant Palin
Grant Palin
You''re passing the Graphics object instead of the green value to the Color constructor. Try:
public void paint (Graphics g) {  int red = 255;  int green = 255;  int blue = 255;  Color color = new Color(red, green, blue);  g.setColor(color);  // Do your drawing commands} 
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Ah-hah! Thanks!!

Grant Palin
Grant Palin

This topic is closed to new replies.

Advertisement