Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Colors in Java

Started by gpalin Dec 3, 2002 at 8:56 PM 7 replies 400+ views
Original Post
gpalin
gpalin
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
johnnyboy
johnnyboy
Basically:

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

g.setColor(Color.c);

Right?

Grant Palin
Grant Palin
Kentaro
Kentaro
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
gpalin
gpalin
Right, I gotcha. Thanks!!

Grant Palin
Grant Palin
gpalin
gpalin
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
WayfarerX
WayfarerX
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
gpalin
gpalin
Ah-hah! Thanks!!

Grant Palin
Grant Palin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.