Solved

colors RGB

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

11 Experts available now in Live!

Get 1:1 Help Now