# Mutable image with transparency

This topic is 5466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. When I create an image with "Image.createImage(width,height)" the image background is white. Is there a way to create a mutable image with an transparent background? This is for Midp 1.0

##### Share on other sites
To the best of my knowledge you cannot create a transparent image just like that in midp1.

Midp 2 you'd have to have a block of transparent pixels and set processAlpha to true.

The way to do this in midp1 would be to have a transparent image and do:

Image source; // the image to be copiedsource = Image.createImage(...); // a transparent imageImage copy = Image.createImage(source.getWidth(),source.getHeight());Graphics g = copy.getGraphics();g.drawImage(source, 0, 0, TOP|LEFT);

You may want to keep the transparent image down to a size of 8x8 or something along those lines and just do:

Image source; // the image to be copiedsource = Image.createImage(...); // a transparent imageImage copy = Image.createImage( /* wanted width here */,/* wanted height here */ );Graphics g = copy.getGraphics();for ( int x = 0; x < source.getWidth(); x+=8) {    for ( int y = 0; y < source.getHeight(); y+=8 ) {        g.drawImage( source, x, y, Graphics.TOP | Graphics.LEFT );    }}

##### Share on other sites
Hi.

It looks like you just draw a transparent image over a white image. The image is still white.

##### Share on other sites
It's just a thought and should work, i havent tested it though. Make sure the background of where you are painting the new image to ISNT white. You'll actually have to draw something else onto the image to be sure it works...

Now why are you doing this? What is the point of an image that is completely transparent, its a waste of memory and processing time.

##### Share on other sites
MIDP 1 does not require support for transparent images *at all*. However, in practice almost all decent MIDP 1 phones provide the support.

In MIDP 2 you have the option to manipulate an image buffer and re-create an Image from it, and produce transparent images. However, there is still no such thing as an image which is both transparent and mutable (and I agree that it is a very stupid limitation on things, I know of hacks that I could do otherwise that would save lots of memory); you need to modify your image data and then create another Image (probably letting the original be garbage-collected).

However, keep in mind that you can never really *show* "a transparent image", because there is no such thing as a "clear" pixel on the LCD. *Something* has to show through.

Thus, *most* of what you are likely to want to do, can be done easily (and it all can be done with some effort) by keeping around an immutable, transparent Image, and drawing its contents on top of other things. You might find it helpful to keep a backbuffer in which you draw things, and then blit the whole buffer to the screen.

Even if the device is double-buffered. This is because you may be able to do some of the more complicated drawing only once, and then leave that stuff in the buffer. Lots of speed optimization available there. Keep in mind that drawing an Image to screen takes time measurable in *milliseconds* on some devices. You can't do too many of those per frame.

##### Share on other sites
Quote:
 Original post by kooktroopIt's just a thought and should work, i havent tested it though. Make sure the background of where you are painting the new image to ISNT white. You'll actually have to draw something else onto the image to be sure it works...

The code you provided is from the api Image class, and has the purpose to create a mutable copy of an immutable image. I am sure this works, but not for my purpose.

Quote:
 Now why are you doing this? What is the point of an image that is completely transparent, its a waste of memory and processing time.

There might be several points. Mine is to save png header space by storing many images in one huge image file. When I extract the individual images from this huge image, I need to draw them onto another image. I could of course draw directly into the screenbuffer, but doing the same operation many times every secund is not my style.

This is how I extract the small image from the huge image:

Image getImageRegion(Image source, int x, int y, int width, int height)

{

Image result = Image.createImage(width,height);
//This is the image I want transparent.
{

if(x + width > source.getWidth() || y + height > source.getHeight())

{

System.out.println("Error");

}

result.getGraphics().drawImage(source, -x, -y,Graphics.TOP|Graphics.LEFT);

return result;

}

}

This works very well for tilemaps, where the tiles don't have any transparenty (at least in my world), but creates as said a white background in the background of the transparent sprites.

##### Share on other sites
Yes i copied it out of the APi docs because thats all you need to do.

It looks like you dont need to render any transparent images at all. Why you want to do this i dont know.

##### Share on other sites
Uh...
Instead of extracting the pieces of the large PNG and then drawing the appropriate piece as a whole image, have you considered just drawing the piece of the large PNG each time you need a piece? Look into Graphics.setClip() to control the area of the target (probably screen?) which is actually drawn to. Set that to be the size of the piece you want to draw, then make a drawImage call, with the big image positioned such that the piece you want to draw is on top of the destination "window". :)

##### Share on other sites
Quote:
 Original post by ZahlmanLook into Graphics.setClip() to control the area of the target (probably screen?) which is actually drawn to. Set that to be the size of the piece you want to draw, then make a drawImage call, with the big image positioned such that the piece you want to draw is on top of the destination "window". :)

Thanx. This sounds reasonable. Will try it as soon as real life give me a break.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 14
• 40
• 63