BufferedImage rotated, change resulting background

前端 未结 1 1632
南方客
南方客 2021-01-22 11:41

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

I want my progr

相关标签:
1条回答
  • 2021-01-22 12:25

    You have (at least) two options...

    You Could...

    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...

    0 讨论(0)
提交回复
热议问题