IT Community - Software Programming, Web Development and Technical Support

How to Make an image transparent in J2ME ?

This is a discussion on How to Make an image transparent in J2ME ? within the J2ME forums, part of the Mobile Software Development category; How to Make an image transparent in J2ME ?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Mobile Software Development > J2ME

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 07-20-2007, 11:14 PM
itbarota itbarota is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 542
itbarota is on a distinguished road
Question How to Make an image transparent in J2ME ?

How to Make an image transparent in J2ME ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 07-21-2007, 12:44 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 631
oxygen is on a distinguished road
Default Re: How to Make an image transparent in J2ME ?

J2ME generally deals with PNG images. These images can be made transparent or semitransparent by changing the alpha channel value of the image matrix accordingly.

The sample midlet code below shows a fade in and fade out effect.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import javax.microedition.io.*;
import java.io.*;

public class Fading extends javax.microedition.midlet.MIDlet
implements CommandListener{

private Command mExitCommand;

public void startApp() {

mExitCommand = new Command("Exit", Command.SCREEN, 0);
TheCanvas tc = new TheCanvas();
tc.addCommand(mExitCommand);
tc.setCommandListener(this);
Display d = Display.getDisplay(this);
d.setCurrent(tc);
}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

public void commandAction(Command c, Displayable d){
notifyDestroyed();
}
}

class TheCanvas extends Canvas
implements Runnable{

private Image mJavaImage;
private Image mBgImage;
private Image mBufferImage;

private Graphics mGraphics;

private boolean mTrucking = true;
private boolean mUpdate = true;

private int mAlpha = 0;
private int mValue = 5;

private int[]rawInt;

private Runtime rt;

public TheCanvas(){

rt = Runtime.getRuntime();

mBufferImage = Image.createImage(getWidth(), getHeight());
mGraphics = mBufferImage.getGraphics();
try{
mJavaImage = Image.createImage("/java.png");
mBgImage = Image.createImage("/bg.png");
}catch(Exception e){}
rawInt = new int[mJavaImage.getWidth() * mJavaImage.getHeight()];
mJavaImage.getRGB(rawInt, 0, mJavaImage.getWidth(), 0, 0, mJavaImage.getWidth(), mJavaImage.getHeight());

Thread t = new Thread(this);
t.start();
}

public void paint(Graphics g){
g.drawImage(mBufferImage, 0, 0, 0);
g.drawString("freeMemory=" + String.valueOf(rt.freeMemory()), 0, 100, 0);
mUpdate = true;
}

public void run() {
while(mTrucking){
if(mUpdate){
mAlpha+= mValue;
if(mAlpha>=255)
mValue = mValue *-1;
else if(mAlpha<=0)
mValue = mValue *-1;

ImageEffect.blend(rawInt, mAlpha);
Image fadingImage = Image.createRGBImage(rawInt, mJavaImage.getWidth(), mJavaImage.getHeight(), true);

mGraphics.setColor(0xFFFFFF);
mGraphics.fillRect(0, 0, getWidth(), getHeight());
mGraphics.drawImage(mBgImage, 0, 0, 0);
mGraphics.drawImage(fadingImage, 0, 10, 0);
System.gc();

mUpdate = false;
}
repaint();
}
}

}

class ImageEffect{
public static void blend(int[] raw, int alphaValue, int maskColor, int dontmaskColor){
int len = raw.length;

for(int i=0; i<len; i++){
int a = 0;
int color = (raw[i] & 0x00FFFFFF);
if(maskColor==color){
a = 0;
}else if(dontmaskColor==color){
a = 255;
}else if(alphaValue>0){
a = alphaValue;
}

a = (a<<24);
color += a;
raw[i] = color;
}
}
public static void blend(int[] raw, int alphaValue){
blend(raw, alphaValue, 0xFFFFFFFF, 0xFFFFFFFF);
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help make a decision str1099 Computer Hardware 0 08-09-2008 04:25 AM
Convert image to other image format using CODEC in .NET 3.0 Mramesh C# Programming 0 02-07-2008 02:33 AM
How to display a windows mobile form with transparent background? $enthil Windows Mobile 1 11-28-2007 09:36 PM
How to create an image from panel background Image S.Vinothkumar C# Programming 1 10-22-2007 02:52 AM
How to set the TextArea component to Transparent oxygen Flash Actionscript Programming 1 07-21-2007 01:47 AM


All times are GMT -7. The time now is 12:26 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
Stamford Dentist | Euro Millions Lottery | Tech Support Services

SEO by vBSEO 3.0.0