/**
* 设置背景图片
*
* @param ii 背景图片
* @param width 背景的宽度
* @param height 背景的宽度
*/
public void setBackgroundByImageIcon(ImageIcon ii, int width, int height) {
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
if (ii != null && ii.getIconWidth() > 0 && ii.getIconHeight() > 0) {
g2d.drawImage(ii.getImage(), 0, 0, width, height, null); // 绘制缩小后的图
}
g2d.dispose();
Rectangle rect = new Rectangle(0, 0, width, height);
TexturePaint texture = new TexturePaint(bufferedImage, rect);
setBackground(new TextureBackground(texture));
}
设置背景图片的Java代码实现
本文介绍如何使用Java代码实现设置背景图片的功能,包括创建BufferedImage对象、使用Graphics2D绘制图片以及设置TexturePaint作为背景。代码示例清晰易懂,适合Java初学者和中级开发者。

600

被折叠的 条评论
为什么被折叠?



