BufferedImage旋转,更改生成的背景 [英] BufferedImage rotated, change resulting background

查看:575
本文介绍了BufferedImage旋转,更改生成的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Graphics2D.rotate()旋转图像时,显然在角落处留有一些空白.空的角变成透明的.

When I rotate an image using Graphics2D.rotate() obviously it leaves some empty space in the corners. The empty corners become transparents.

我希望我的程序旋转BufferedImage并用白色填充其余的空白角.

I want my program to rotate the BufferedImage and to fill the remaining empty corners with a white color.

我该怎么做?

换句话说,我想在旋转图像的同时为整个图像保留白色背景.

In other words, I want to rotate the image while preserving a white background for the whole image.

这是我的功能:

public BufferedImage rotateImage(BufferedImage image, double degreesAngle) {    
        int w = image.getWidth();    
        int h = image.getHeight();    
        BufferedImage result = new BufferedImage(w, h, image.getType());  
        Graphics2D g2 = result.createGraphics();  
        g2.rotate(Math.toRadians(degreesAngle), w/2, h/2);
        g2.drawImage(image,null,0,0);  
        return result;   
    }    

然后我使用此图像将其绘制为透明的JPanel,然后将其添加到JLayeredPane中.

I then use this image to paint it a transparent JPanel, which I later add to a JLayeredPane.

但是,我的图像有透明的角,我想用白色填充它们.

However, my image has transparent corners and I want to fill them with a white color.

推荐答案

您(至少)有两个选择...

You have (at least) two options...

在绘制旋转的图像之前,先绘制BufferedImage的背景...

Paint the background of the BufferedImage before you paint the rotated image...

public BufferedImage rotateImage(BufferedImage image, double degreesAngle) {    
    int w = image.getWidth();    
    int h = image.getHeight();    
    BufferedImage result = new BufferedImage(w, h, image.getType());  
    Graphics2D g2 = result.createGraphics();  
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, w, h);
    g2.rotate(Math.toRadians(degreesAngle), w/2, h/2);
    g2.drawImage(image,null,0,0);  
    return result;   
}  

您可以...

在将图像绘制到面板上之前,先绘制图像后面的区域...

You Could...

Paint the area behind the image before you paint it to the panel...

这篇关于BufferedImage旋转,更改生成的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆