Solved

colors RGB

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

860 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