Hey folks, I am working on a j2ME game for java-capable cell phones. I am attempting to scale a transparent PNG with the following method:
// method derived from a Snippet from http://snippets.dzone.com/posts/show/3257
// scales an image according to the ratios given as parameters
private Image rescaleImage(Image image, double XRatio, double YRatio)
{
// the old height and width
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
// what the new height and width should be
int newWidth = (int)(XRatio * sourceWidth);
int newHeight = (int)(YRatio * sourceHeight);
Image newImage = Image.createImage(newWidth, newHeight);
Graphics g = newImage.getGraphics();
for (int y = 0; y < newHeight; y++)
{
for (int x = 0; x < newWidth; x++)
{
g.setClip(x, y, 1, 1);
int dx = (x * sourceWidth) / newWidth;
int dy = (y * sourceHeight) / newHeight;
g.drawImage(image, (x - dx), (y - dy), Graphics.LEFT | Graphics.TOP);
}
}
return Image.createImage(newImage);
}
It scales the image correctly, unfortunately I seem to be losing the transparency with the image returned by the method. I am fairly new to these concepts, and any help would be greatly appreciated! Please note that in order to be properly displayed on any java-capable mobile device, the rescaling needs to be done in code, not in any sort of image editor.
Thanks in advance!