求大神帮助,谢谢!强制转化问题。

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
    Graphics2D g2 = (Graphics2D)image.getGraphics(); 
    g2.setColor(this.bgColor);
    g2.fillRect(0, 0, w, h);
    return image;
}
第二个语句为什么要用强制转化

getGraphics获取的是Graphics类型,Graphics2D是Graphics的子类,向下转型需要强转。

getGraphics()方法声明中返回Graphics类型,而方法返回Graphics2D,是出于向后兼容性的考虑。 你可以使用createGraphics,它更为方便,因为它被声明为返回 Graphics2D