Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load solid image, then make it transparent?

Status
Not open for further replies.

TeknomanSlade

Programmer
May 19, 2002
5
0
0
US
How's it goin' folks? Got a new question for you...

I'm working on a complex animation that deals with sprite-style
images. I've got one sprite right now that's saved as a .PNG that's
got about 800 colors in it (according to PSP's "Count colors" function).
It's made to blend in with the background, so I can't degrade it into
a transparent .GIF without it ending up looking bad, so here's my question:

I've got the image itself surrounded by a solid green area (kind of like,
if it were a .GIF, the green area would be transparent). What I'd like
to know is, is there a way to load the .PNG into a graphics space, then
paint the green area with a color that registers as blank/transparent/null
with Java, and save that resulting picture back to the original image so
I can draw it into my scene without any trouble?

I'll drive my point home with some pseudo-code here...I'm aware it looks like
crap, but...:
Code:
Image TheObject;
Image FinalObject;
Graphics workspace;

public void init() {
TheObject = getImage(getCodeBase(), "object.png");
FinalObject = createImage(TheObject.Width, TheObject.Height);
workspace = FinalObject.getGraphics();
workspace.drawImage(TheObject,0,0,this);

// Paint the green area so that it's transparent...
workspace.floodfill(0,0,color.transparent);

// Replace original image with the new transparent one...
TheObject = FinalObject;
}

public void paint(Graphics g) {
// Draw new object three times.
g.drawImage(TheObject,0,0,this);
g.drawImage(TheObject,50,50,this);
g.drawImage(TheObject,100,100,this);
}
Can this be done?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top