Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Color representation in Java

Wed Oct 07, 2009 1:55 pm

Hi everybody!
I am trying to color some lines gradually, depending on one variable, speed, namely if the value of the variable is really small I need a strong color otherwise a more blurry one, even white.
The variable I have is speed so it is varying between 20 and 120 usually.
I tried two ways for doing this but non of them is working :-S All I get is a black color a bit less darker when speed decreases, but u can hardly see the difference.
First try :
float hue = (float) 0.6;
float saturation = (float) (1.0 - speed / 130.0);
float brightness = (float) (1.0 - speed / 130.0);
g2d.setColor(Color.getHSBColor(hue, saturation, brightness));
width = 10;

Second:
g2d.setColor(new Color((float) ((1.0 - speed / 130.0)*(1.0 - speed / 130.0)), (float) (1.0 - speed / 130.0), (float) (1.0 - speed / 130.0)));

I would really appreciate any advice!



Re: Color representation in Java

Wed Oct 07, 2009 5:44 pm

I think you have to change only the Saturation


java code
package test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;

public class HSBColors extends JFrame{

public HSBColors()
{
setTitle("HSBColor Frame");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {

super.paint(g);
Graphics2D graphics2D = (Graphics2D) g;
float h = 0f ;
float s = 0.9f ;
int b = 1 ;
graphics2D.setColor(Color.getHSBColor(h, s, b));
graphics2D.fillRect(10, 10, 300, 300);
}
public static void main(String[] args)
{
new HSBColors().setVisible(true);
}
}

Attachments
hsb.GIF
hsb
hsb.GIF (10.09 KiB) Viewed 5246 times

Post a reply
  Related Posts  to : Color representation in Java
 Gradient Color in java     -  
 java Color Components     -  
 Changing the color of a graph draw in java     -  
 Ball Color Match 2D Java Game Version 1.0     -  
 Color and CSS     -  
 Changing link color     -  
 Draw char with color in php     -  
 change background color with RGB     -  
 skymiles background color     -  
 Set border bottom color     -