Hello!
I would like to do next thing.
Currently i blit some textures on screen. The textures are designed for highest resolution application will support. I would like to have/program method which will, when i start application, resize the Texture to a size, which will fit configured resolution. Since the Textures must be n^2 in size. The actuall size of image shouldn't be resized. Only content of image. I'll blit then only the part of texture.
Any hints?
If you are creating the textures from images, you can use Java2D scaling functions to do that.
I figurated it out.
public static BufferedImage scaleImage(BufferedImage image, double factorW, double factorH)
{
BufferedImage scaled = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(factorW, factorH), null);
image = op.filter(image, null);
scaled.getGraphics().drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
return scaled;
}
This works too.
Image image = imageBig.getScaledInstance(newWidth, -1,Image.SCALE_FAST);
If you change the SCALE_ to something else, you can get a higher quality scale.
The thing is I needed to maintain original size of image and make smaller content. The part with no content is left opaque.
I'm sure your way will work fine:)
I would just have a feeling the method I suggested would be slightly faster.
If you got rid of the affine transform, you could just paint it onto the buffered image.
public static BufferedImage scaleImage(BufferedImage image, int scaleWidth, int scaleHeight)
{
int width = image.getWidth();
int height = image.getHeight();
BufferedImage scaled = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Image imageSmall = image.getScaledInstance(scaleWidth, scaleHeight,Image.SCALE_FAST);
scaled.getGraphics().drawImage(imageSmall, 0, 0, null);
return scaled;
}
Speed is not a factor here, since I only scale a few images at initialization. Works fast already. So I'll stick with how it is. ;). No need for optimization :mrgreen:
I changed scaling to how you suggested. It resizes texture much smoother.
public static Image scaleImage(BufferedImage image, double factorW, double factorH)
{
BufferedImage scaled = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
int width = (int)(factorW*image.getWidth());
int height = (int)(factorH * image.getHeight());
Image img = image.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
scaled.getGraphics().drawImage(img, 0, 0, width, height, null);
return scaled;
}
I'm glad it works for you:).