我尝试创建一个包含BufferedImage的类,并且可以自行旋转。但是,如果我尝试在临时主方法的循环中访问它,它会产生内存泄漏,我无法负责任地解决它。
public class PImage
{
private BufferedImage image;
@SuppressWarnings("rawtypes")
private static Class ressourceClass = PImage.class;
public PImage(BufferedImage setImage)
{
image = setImage;
}
public PImage(String name)
{
load(name , "png");
}
private void load(String name , String extension)
{
String path = "/" + name + "." + extension;
try
{
image = ImageIO.read(ressourceClass.getResource(path));
}
catch (IOException e)
{
System.out.println("PImage: Ladefehler. " + path + " existiert nicht.");
}
}
public PImage newRotate(double angle)
{
angle = angle % (2 * Math.PI);
int newHeight = 1 + (int) Math.sqrt(Math.pow(image.getWidth() , 2) + Math.pow(image.getHeight() , 2));
int newWidth = 1 + (int) Math.sqrt(Math.pow(image.getWidth() , 2) + Math.pow(image.getHeight() , 2));
BufferedImage newImage = new BufferedImage(newWidth , newHeight , BufferedImage.TYPE_4BYTE_ABGR);
AffineTransform affineTransform = new AffineTransform();
AffineTransformOp rotateOP;
affineTransform.translate((newWidth - image.getWidth()) / 2 , (newHeight - image.getHeight()) / 2);
affineTransform.rotate(angle , image.getWidth() / 2 , image.getHeight() / 2);
rotateOP = new AffineTransformOp(affineTransform , AffineTransformOp.TYPE_BILINEAR);
newImage = rotateOP.filter(image , newImage);
return new PImage(newImage);
}
public void rotate(double angle)
{
image = newRotate(angle).getImage();
}
public static void main(String[] args)
{
PImage i = new PImage("test");
while (true)
{
i.rotate(0.1);
}
}