Reputation: 297
I am using java swing to create an interface where the user draws a couple of points. What I want to do is after those points are drawn to change automatically the intensity of the color from really bright to dark till the point fades. Does anyone know any tutorials on how to change the color intensity because I cannot find something to help me.
EDIT: Thank you for your answers they helped me understand better how to work with Color class. I fixed it so I am uploading the thread part to help if anyone anyone else needs to do something similar... I am working on a black background so I darken the colors instead of lightening them.
public class MyThread extends Thread {
private Canvas canvas;
private int sleepingTime = 5000;
private Color color;
private int red, green, blue, alpha;
public MyThread(Canvas canvas) {
super();
this.canvas = canvas;
setDaemon(true);
}
public void run(){
while (true){
try {
System.out.println("going to sleep...");
Thread.sleep(sleepingTime);
} catch (InterruptedException e) {
System.out.println("sleep interrupted..");
return;
}
System.out.println("woke up!");
int size = canvas.points_list.size();
int i =0;
while (size > 0) {
color = canvas.points_list.get(i).getForeground();
red = (int) Math.round(Math.max(0, color.getRed() - 255 * 0.25f));
green = (int) Math.round(Math.max(0, color.getGreen() - 255 * 0.25f));
blue = (int) Math.round(Math.max(0, color.getBlue() - 255 * 0.25f));
alpha = color.getAlpha();
canvas.points_list.get(i).setForeground(new Color(red, green, blue, alpha));
size--;
i++;
}
canvas.repaint();
}
}
}
Upvotes: 0
Views: 1202
Reputation: 12259
Take a look at the HSB color model. It allows you to specify a "intensity" (saturation and brightness).
Example (red background becoming black - alternatively, modify the "s" parameter for fading the color to white):
final Frame frame = new Frame();
Timer timer = new Timer(500, new ActionListener() {
float b = 1.0f;
@Override
public void actionPerformed(ActionEvent e) {
int color = Color.HSBtoRGB(0, 1, b);
frame.setBackground(new Color(color));
b -= 0.05f;
}
});
timer.start();
frame.setSize(200, 200);
frame.setVisible(true);
Upvotes: 0
Reputation: 664
You need to use the RGB form to make it as a gradient from bright to dark by adding blue and green and keeping the red value the same, do this continually in a for loop then at the end set it to you background color making it invisible.
Upvotes: 0