hi,
i made a test where i draw a volatile image over another volatile image (or just on the current graphics). previously i used bufferedimages, which worked fine (i use a gif with transparency). but now i switched to volatile images the bitmasked transparency is ignored. the source is posted below, i’d greatly appreciate it if somebody could point out things that are done wrong and of course what causes this
import java.applet.;
import java.net.;
import java.awt.;
import java.awt.image.;
public class VolatileTest extends Applet implements Runnable
{
private Thread thisThread=null;
private int awidth=512, aheight=512;
private Graphics gfx = null;
public GraphicsConfiguration gc = null;
private Image buffer, sprite;
public void init()
{
try
{
awidth = Integer.parseInt(getParameter("width"));
aheight = Integer.parseInt(getParameter("height"));
Image backGround = getImage(getClass().getResource("back.gif"));
Image testSprite = getImage(getClass().getResource("sprite.gif"));
buffer = createVolatileImage(awidth,aheight,1);
sprite = makeVolatileImage(testSprite);
setIgnoreRepaint(true);
}
catch (Exception e) { e.printStackTrace(); }
gfx = getGraphics();
}
public Image makeVolatileImage(Image orig) throws Exception
{
waitForImage(orig);
int width = orig.getWidth(null);
int height = orig.getHeight(null);
int imgBytes = width * height * 2; //nr pixels * 2bytes color information per pixel
int bytesAvailable = checkVolatileMem();
if (bytesAvailable < 0 || (bytesAvailable > 0 && bytesAvailable < imgBytes))
throw new Exception("not enough volatile memory available (required: " + imgBytes + " / available: " + bytesAvailable);
int transparency = hasAlpha(orig);
Image image = createVolatileImage(width,height,transparency);
Graphics graphics = ((VolatileImage)image).createGraphics();
graphics.drawImage(orig, 0, 0, this);
graphics.dispose();
return image;
}
protected void waitForImage(Image image)
{
MediaTracker mediatracker = new MediaTracker(this);
mediatracker.addImage(image, 0);
try
{
mediatracker.waitForAll();
}
catch (Exception _ex) { }
}
public int hasAlpha(Image image)
{
if (image instanceof BufferedImage)
{
BufferedImage bimage = (BufferedImage)image;
return bimage.getColorModel().getTransparency();//hasAlpha();
}
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try
{
pg.grabPixels();
}
catch (InterruptedException e)
{
}
ColorModel cm = pg.getColorModel();
return cm.getTransparency();//hasAlpha();
}
public int checkVolatileMem()
{
int bytes=-1;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try
{
GraphicsDevice[] gs = ge.getScreenDevices();
for (int i = 0; i < gs.length; i++)
{
VolatileImage im = gs[i].getDefaultConfiguration().createCompatibleVolatileImage(1, 1);
bytes = gs[i].getAvailableAcceleratedMemory();
im.flush();
}
}
catch (HeadlessException he) {
}
return bytes;
}
public Image createVolatileImage(Image im)
{
waitForImage(im);
int transparency = hasAlpha(im);
Image temp = createVolatileImage(im.getWidth(null), im.getHeight(null), transparency);
Graphics graphics = ((VolatileImage)temp).createGraphics();
graphics.drawImage(im, 0, 0, this);
graphics.dispose();
return temp;
}
public Image createVolatileImage(int width, int height, int transparency) {
if(gc == null) getGC();
return gc.createCompatibleVolatileImage(width, height, transparency);
}
protected void getGC()
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gc = gs.getDefaultConfiguration();
}
public void start() {
thisThread = new Thread(this);
thisThread.start();
}
public void paint(Graphics g)
{
//Graphics g2 = buffer.getGraphics();
g.setColor(Color.red);
g.fillRect(0,0,awidth,aheight);
g.drawImage(sprite,0,0,this);
//g.drawImage(buffer,0,0,this);
Toolkit.getDefaultToolkit().sync();
}
public void run()
{
while(Thread.currentThread() == thisThread)
{
paint(gfx);
try {
Thread.sleep(50);
}
catch(Exception e) {}
}
}
}