?
Solved

colors RGB

Posted on 2004-10-09
5
Medium Priority
?
383 Views
Last Modified: 2010-03-31
hi experts,

the only way I know to use colors is to say for example Color.white which doesn't have much range. I know there is some way to set colors using RGB or if you suggest a better way

please include example

thanks
0
Comment
Question by:Samooramad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Accepted Solution

by:
TrekkyLeaper earned 200 total points
ID: 12268389
You can create Color objects using the other constructors. For example:

float r = <red value from 0 - 1.0>;
float g = <green value>;
float b = <blue value>;
Color myColor = new Color( r, g, b );

You can also do this with the standard 0-255 integers and numerous other ways. See the javadocs for the other constructors:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 200 total points
ID: 12268450
0
 
LVL 2

Assisted Solution

by:shah1d1698
shah1d1698 earned 200 total points
ID: 12271013
try this...

import java.awt.*;
import javax.swing.*;

class ABC extends JFrame
{
  public ABC()
  {
        Container con = getContentPane();
        setVisible(true);
      setSize(500, 500);
  }

  public void paint(Graphics g)
  {
       g.setColor(new Color(255, 0, 0));//complete red
       g.drawLine(0, 0, 300, 100);
      
       g.setColor(new Color(0, 255, 0));//complete green
       g.drawLine(0, 0, 300, 200);
      
       g.setColor(new Color(0, 0, 255));//complete blue
       g.drawLine(0, 0, 300, 300);
      
        g.setColor(new Color(255, 255, 255));//white
        g.drawLine(0, 0, 300, 400);
      
        g.setColor(new Color(0, 0, 0));//black
        g.drawLine(0, 0, 300, 450);

     //i.e, u can use the format new Color(red, green, blue)
     //where red, green, blue are in the range of 0 to 255.
    //U can use any combination of these to create your
    //own color. For example-

      g.setColor(new Color(95, 50, 150));
      // here ratio of blue will be
      //greater than the others.
      g.drawLine(0, 0, 300, 500);

  }

  public static void main(String[] aaaa)
  {
    ABC abc = new ABC();
    abc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

}
0
 
LVL 1

Assisted Solution

by:phavardel
phavardel earned 200 total points
ID: 12271989
There are several options contained within the Color class. I find the simplest option:

public Color(int red, int green, int blue)

where each int is a value between 0-255. Not only is this clean, but you can use your graphics software (even just MS Paint) to find a good color and it'll tell you the RGB value for it.

For example, paint tells me that a deep purple is red=174, green=56, blue=199. So, all you'd need to do is say

Color deepPurple = new Color(174, 56, 199);

and then just reference the variable whenever you want that color (i.e. g.setColor(deepPurple))

Other options include:

Color(int rgb) where all the weights are stored in a single integer variable, red being bits 23 downto 16, green 15 downto 8, and blue 7 downto 0

Color(float r, float g, float b) - similar to the int one I used above, but instead each value is between 0.0 and 1.0


0
 

Author Comment

by:Samooramad
ID: 12273017
thanks everyone you were very helpful
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question