Solved

colors RGB

Posted on 2004-10-09
5
376 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now