1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Colors in Java

  1. Apr 29, 2009 #1
    I'm trying to set the color using three integer values for R, G, and B stored in three int type variables (these values are randomly generated between 0 and 255).

    Previously we set the color (e.g. yellow) using g.setColor(Color.YELLOW).

    I'm unsure of how to do this. The course is online, and I've read the text section multiple times.
  2. jcsd
  3. Apr 29, 2009 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Look at the API documentation for whatever class is the object g belongs to. There will be a number of overloaded methods, call called setColor, but which accept different arguments. One of them may accept a Color object, and that should prompt you to look at the documentation of the Color class. You will find that you can construct a Color object with three arguments, red, green, and blue.

    - Warren
  4. Apr 29, 2009 #3
    Hmmm... I've looked around the API and can't seem to find the code needed to set the color using RGB values.

    Knew I shouldn't have taken an online class... :mad:
  5. Apr 29, 2009 #4
    http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html [Broken]

    You are looking for this constructor:
    Color(float r, float g, float b)
    Last edited by a moderator: May 4, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook