import sun.java2d.SunGraphics2D; //导入方法依赖的package包/类
/**
* Return a non-accelerated BufferedImage of the requested type with the
* indicated subimage of the original image located at 0,0 in the new image.
* If a bgColor is supplied, composite the original image over that color
* with a SrcOver operation, otherwise make a SrcNoEa copy.
*
* Returned BufferedImage is not accelerated for two reasons:
*
*
Types of the image and surface are predefined, because these types* correspond to the TransformHelpers, which we know we have. And
* acceleration can change the type of the surface
*
Image will be used only once and acceleration caching wouldn't help*
*/
private BufferedImage makeBufferedImage(Image img, Color bgColor, int type,
int sx1, int sy1, int sx2, int sy2)
{
final int width = sx2 - sx1;
final int height = sy2 - sy1;
final BufferedImage bimg = new BufferedImage(width, height, type);
final SunGraphics2D g2d = (SunGraphics2D) bimg.createGraphics();
g2d.setComposite(AlphaComposite.Src);
bimg.setAccelerationPriority(0);
if (bgColor != null) {
g2d.setColor(bgColor);
g2d.fillRect(0, 0, width, height);
g2d.setComposite(AlphaComposite.SrcOver);
}
g2d.copyImage(img, 0, 0, sx1, sy1, width, height, null, null);
g2d.dispose();
return bimg;
}