Setting Color Using Int Variables

  • Context: Comp Sci 
  • Thread starter Thread starter Nick M
  • Start date Start date
  • Tags Tags
    Color Variables
Click For Summary

Discussion Overview

The discussion revolves around how to set a color using three integer values representing red, green, and blue (RGB) in a programming context, specifically within an online course setting. Participants explore the use of a method to set color based on these RGB values.

Discussion Character

  • Homework-related, Technical explanation

Main Points Raised

  • One participant expresses confusion about how to set the color using RGB integer values after previously using predefined colors.
  • Another participant suggests consulting the API documentation for the relevant class to find overloaded methods for setting color, indicating that a Color object can be constructed with RGB values.
  • A different participant reports difficulty in locating the necessary code in the API documentation and expresses frustration with the online course format.
  • A further reply provides a link to the API documentation and identifies a specific constructor for the Color class that accepts float parameters for RGB values.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the specific method to use, as one expresses confusion while others provide guidance. The discussion remains unresolved regarding the exact implementation details.

Contextual Notes

There is uncertainty regarding the correct method to convert integer RGB values to the required format for the Color constructor, as well as potential confusion about the API documentation's clarity.

Nick M
Messages
191
Reaction score
0
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.
 
Physics news on Phys.org
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
 
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:
 
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html

You are looking for this constructor:
Color(float r, float g, float b)
 
Last edited by a moderator:

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K