# Thread: Transparant Rectangle, possible ?

1. ## Transparant Rectangle, possible ?

Hi all,
I wanted to paint a filled rectangle with transparency on my Canvas ?
Specifically like a semi-transparent rectangle, so not completely transparent...
Any idea's on how to succeed ? I've been searching on the net but I haven't found any real answers regarding this. Afaik it's not possible directly through the Graphics class, simply because using the g.setColor() method only the RGB values are passed and no aplha...

Any tricks maybe ? like using a 1 by 1 pixel Image and keep copying this till u have a rectangle ?

Im anxious to hear any input regarding this issue,

2. ## Re: Transparant Rectangle, possible ?

Hi this work only in MIDP 2

public static final void drawAlphaRect(Graphics g, byte alpha, int color, int borderColor, int x, int y, int w, int h)
{
int size = w * h;
int[] alphaRGB = new int[size];
for( int i = 0; i < size; ++i )
alphaRGB[i] = color|(alpha<<24);

Image alphaBack = Image.createRGBImage(alphaRGB, w, h, true);
g.drawImage(alphaBack, x, y, 0);

g.setColor(borderColor);
g.drawRect(x, y, w, h);
}

3. ## Re: Transparant Rectangle, possible ?

thx,
just one question about the supplied arguments to the method :

Graphics g == the graphics object
byte alpha == please could you explain this one to me ? values and effects ?
int color == I guess this is the (partly) transparant color
int borderColor == the color of the border of the rectangle ?
int x == starting x coordinate
int y == starting y coordinate
int w == width
int h == height

Could u please explain the alpha (maybe show an example for a 80% transparant alpha value) and the color variable (guess like white would be 0xFFFFFF?) ?

4. ## Re: Transparant Rectangle, possible ?

thx for the help !
In the end I managed by creating a byte alpha mask in the following way

byte neededAlpha = (byte)200; //200 out of 255

and for the passed color as in my previous post (0xffffff for white for example).

5. ## Re: Transparant Rectangle, possible ?

byte alpha: you should use 0x00 to 0xFF to get the diferents values of alpha transparency

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•