Solved

colors RGB

Posted on 2004-10-09
5
381 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 50 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 50 total points
ID: 12268450
0
 
LVL 2

Assisted Solution

by:shah1d1698
shah1d1698 earned 50 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 50 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

724 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