Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gpalin

[java] Colors in Java

This topic is 5772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
g.setColor(new Color(r,g,b));

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!