[java2D] colored gradient

M

markus brosch

Hi *

I am looking for a solution for a "rainbow" colored gradient.

At the moment I have a white ... black solution which represents values
from 0 to 100 (new GradientPaint(x,y,Color.black,Color.white))

I want to try to use a rainbow color gradient, because it is more easy to
guess the values in between white and black. I hope you know what I
mean by rainbow color gradient ...

How can I set up such a gradient?
Anyway, if this question is to unprecise, let me know ;-)

Cheers, Markus
 
M

markus brosch

GradientPaint redToYellow =
new GradientPaint(x,y,Color.RED,x,y,Color.YELLOW));
GradientPaint yellowToGreen =
new GradientPaint(x,y,Color.YELLOW,x,y,Color.GREEN));
etc. etc.

Thanks!

I want to map Integer values between 0 and 100 and this was
actually in my white/black solution quite simple. Now it's a little bit
more work, because I have to map only parts of it to the right color. But
it should't be that hard ;-)

Best regards!
 
S

S. Balk

I want to try to use a rainbow color gradient,
How can I set up such a gradient?



private TexturePaint rainbowTexture = null;

public void initRainbowTexture()
{
BufferedImage buffer = ...;
Graphics2D gBuffer = image.createGraphics();

GradientPaint redToYellow =
new GradientPaint(x,y,Color.RED,x,y,Color.YELLOW));
GradientPaint yellowToGreen =
new GradientPaint(x,y,Color.YELLOW,x,y,Color.GREEN));
etc. etc.

gBuffer.setPaint(redToYellow);
gBuffer.fill(new Rectangle(x,y,w,h));

gBuffer.setPaint(yellowToGreen);
gBuffer.fill(new Rectangle(x,y,w,h));
etc. etc.

gBuffer.dispose();
}



public void paintComponent(Graphics g)
{
super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

if(rainbowTexture == null)
{
this.initRainbowTexture();
}

Shape graph = ...;
g2d.setPaint(rainbowTexture);
g2d.fill(graph);

g2d.dispose();
}
 
T

Thomas Weidenfeller

markus brosch said:
I want to try to use a rainbow color gradient, because it is more easy to
guess the values in between white and black. I hope you know what I
mean by rainbow color gradient ...

Yes, I think so. If I remember the details correctly, you get one by
using multiple gradients, going from rainbow-color to rainbow-color.
However, the exact number and type of colors in such a gradient is not
"standardised". E.g. you can get one by using four gradients and going
from blue to magenta to red to orange and to yellow (popular in some
scientific applications). Or use the more classic order: red, orange,
yellow, green, blue, (dark) magenta. And that still leaves anil
(indigo) out from the classic list of the seven colors of the rainbow,
which is supposed to come between blue and magenta.

/Thomas
 
S

S. Balk

For my black and white gradient I just use this (no real mapping):
int rgb = 255 - (value * 255); // value from 0 to 1 (0 to 100%)
g2d.setColor(new Color(rgb, rgb, rgb)); // grey between white and black

use HSB colors, changing the H(ue)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top