[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ProgSoc] Re: java 1.02 question: How to create transparent Image.





Dear Sharil,


	I've got a really ugly hack that does that.  I make no 
representations as to the fitness of this code, but...

	First you need to create an ordinary offscreen drawable image.  Then 
you color that entire offscreen image white with a fillRect(.  Then you draw 
your text on that image in black.  So now you have black text on a white 
background.  Then you do the following:


Color col = /* Your color here. */ 
ImageFilter makeBGclearfilter = new GeoColorFilter( Color.black , col );
Image img = in.createImage(new FilteredImageSource(im.getSource(),
	makeBGclearfilter));
System.out.println( "Left Make Image..." );
return( img );
 

with your image (here called "im") 
to get a second image "img" with a transparent background, with
your "sprite" as color "col", where the class GeoColorFilter is 
the following:

 
 
import java.awt.*;
import java.awt.image.*;
 
public class GeoColorFilter  extends  RGBImageFilter  {
	protected static final int rgbMask	= 0x00ffffff;
	protected static final int alphaMask	= 0xff000000;
	protected static final int cmask	= 0x00c0c0c0;
 
	protected int from , to;
 
	public  GeoColorFilter( Color frm, Color t )  {
		super();
		from = frm.getRGB() & rgbMask;
		to = t.getRGB() & rgbMask;
		//  The  filter's  operation  does  not  depend  on  the
		//  pixel's  location,  so  IndexColorModels  can  be
		//  filtered  directly.
		canFilterIndexColorModel  =  true;
	}
	public  int  filterRGB(int  x,  int  y,  int  rgb)  {
		return  ( /* ((rgb & cmask) == ( from & cmask ) ) */
			( ( rgb & cmask ) != cmask )
			? (to | (rgb & alphaMask)) : rgbMask );
		}
}
 
 
Believe it or not, this code can actually work.


Sincerely,


Thorn Green

 

On Thu, 15 Apr 1999, Sharil Shafie wrote:

> HI...
>    Can any of you help me with this..
>  
> I want to make an image of text. I'll create a new image, using
> createImage(...), and then draw text on it, using drawString(...). But
> the thing is I want to make the background transparent. So then I'll
> call an imagefilter which will find pixels of the background of the
> image and change the alpha value of the pixel to transparent.
>    It's slow rite.. And inefficient.
> Does any of you have any Idea to for even faster methods? CAn U tell me
> how? By the way.. I am using Image because I've got to manipulate the
> image after that. So please suggest me only the method using Image.
> 
> Thanks In Advance.
> Best Regards,
>    Boyi.  
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @nospam.yahoo.com address at http://mail.yahoo.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: swing-unsubscribe@nospam.eos.dk
> To subscribe, e-mail: swing-subscribe@nospam.eos.dk
> For additional commands, e-mail: swing-help@nospam.eos.dk
> Archive at http://www.eGroups.com/list/swing
> 
> 
--
You are subscribed to the progsoc mailing list. To unsubscribe, send a
message containing "unsubscribe" to progsoc-request@nospam.progsoc.uts.edu.au.
If you are having trouble, ask owner-progsoc@nospam.progsoc.uts.edu.au for help.

This list is archived at <http://www.progsoc.uts.edu.au/lists/progsoc/>