2011-11-28 89 views
1

我正在制作带有自定义文本图标的游戏,我想从一个图像加载每个图像。我怎样才能将一张图片分成一大堆其他图片,这些图片只是原始图片的一部分?将图像解析为多个图像

+0

还要考虑'getSubimage()',显示[here](http://stackoverflow.com/a/3078354/230513)。 – trashgod

回答

2

我认为你正在寻找CropImageFilter

一个ImageFilter类,用于裁剪图像。此类扩展的基本ImageFilter类提取现有图像的一个给定的矩形区域和用于容纳只是所提取的区域

新图像提供源时,必须与FilteredImageSource

此使用它类是ImageProducer接口的实现,它接受现有图像和过滤器对象,并使用它们为原始图像的新过滤版本生成图像数据。

例如,

public class Part extends JPanel { 
    private Image src; 

    public Part(Image src) { 
     this.src = src; 
    } 

    public Image create(int xPos, int yPos, int width, int height) { 
     ImageFilter cropImagefilter = new CropImageFilter(xPos, yPos, width, height); //see constructor detail 
     FilteredImageSource filteredImageSource = new FilteredImageSource(this.src.getSource(), cropImagefilter) 
     Image part = createImage(filteredImageSource); 
     return part; 
    } 
} 
+0

感谢那就是我需要的 – blazingkin

+0

你在哪里得到这个createImage函数? – blazingkin

+0

@blazingkin:不客气。它在这里:http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html#createImage%28java.awt.image.ImageProducer%29 –