Hi,
We all see the soft colors on webpages now-a-days. Thats what I am
trying to do to for my JPanel's.
Consider the code
...
JPanel jPanel1 = new JPanel() ; // jPanel
JPanel jPanel2 = new JPanel() ; // Another jPanel
...
// this doesnt work, a new Color object in setBackground method
// Have changed alpha and the 3 colors to
(204,102,102,0),(204,102,102,1)
// Have tried different combos too!
jPanel1.setBackground(new Color(0, 0, 0, 1)) ;
// this works, gives me red.
jPanel2.setBackground(Color.red) ;
I dont know what I am doing wrong. It should work, but I dont get
those lovely soft colors.
For example 99CCCC(R,G,B) in HEX tranlates to 153,204,204 . When I put
those values for the Color object with alpha between 0 and 1, it
doesnt work!
Any help will be greatly appreciated.
Sincerely,
v796.
kodonne2 - 21 Jan 2004 08:24 GMT
I don't know what kind of error that you are getting but this seems to work fine for me - it turns to kind of a teal color for my background. Is this correct? I don't see any difference in yours and my code either. Maybe you're setting the color for your background in more than one place?
sorry I can't be of more help.
kodonne2 - 21 Jan 2004 09:29 GMT
I don't know what kind of error that you are getting but this seems to work fine for me - it turns to kind of a teal color for my background. Is this correct? I don't see any difference in yours and my code either. Maybe you're setting the color for your background in more than one place?
sorry I can't be of more help.
Jim Sculley - 21 Jan 2004 12:39 GMT
> Hi,
> We all see the soft colors on webpages now-a-days. Thats what I am
[quoted text clipped - 19 lines]
> those values for the Color object with alpha between 0 and 1, it
> doesnt work!
If you read the documentation of the Color class, you will see that the
two constructors that accept an alpha argument expect all arguments to
be floats between 0.0f and 1.0f inclusive or integers between 0 and 255
inclusive. By specifying 0 or 1 for your alpha component, you are
telling the color to be nearly 100% transparent. Therefore, you are
seeing the color of the Component under the JPanel.
Jim S.

Signature
Remove my extraneous mandibular appendages to reply via e-mail
v796 - 21 Jan 2004 22:17 GMT
> If you read the documentation of the Color class, you will see that the
> two constructors that accept an alpha argument expect all arguments to
[quoted text clipped - 4 lines]
>
> Jim S.
Yes, you nailed it! It was the alpha. I did read the docs. Thought had
to put in the alpha(0 thru 255). I omitted it and now get what I want.
So now when should we use alpha? And if both 0 an 1 cause transparency
whats the difference? And when to use what?
Thanks to the posters -- kodonne2 and Jim S.
Sincerely,
v796
Jim Sculley - 22 Jan 2004 01:46 GMT
>>If you read the documentation of the Color class, you will see that the
>>two constructors that accept an alpha argument expect all arguments to
[quoted text clipped - 9 lines]
> So now when should we use alpha? And if both 0 an 1 cause transparency
> whats the difference? And when to use what?
0 and 1 are eeffectively the same. The transparency can range from 0 to
255. 0 is fully transparent, 255 is fully opaque. Here's a simple
example. It paints the background white, and then paints over it with
red. The alpha componment of the red color is increased from 0 to 255.
You will see the white at first, and then the red will increase in
intensity (due to the increasing alpha) until the whole panel is red.
import java.awt.*;
import javax.swing.*;
public class AlphaPanel extends JPanel {
private int red;
private int green;
private int blue;
private int currentAlpha;
private long delay;
public AlphaPanel(Color c, long delayInMillis) {
this.red = c.getRed();
this.green = c.getGreen();
this.blue = c.getBlue();
this.delay = delayInMillis;
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(delay);
repaint();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
});
t.start();
}
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(Color.white);
g.fillRect(0,0,w,h);
g.setColor(new Color(red, green, blue, currentAlpha));
g.fillRect(0,0,w,h);
if (currentAlpha == 255) {
currentAlpha = 0;
} else {
currentAlpha++;
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new AlphaPanel(Color.RED, 10));
f.setSize(400,300);
f.setVisible(true);
}
}