当我将图像另存为 JPG 时,为什么会出现黑色背景?

发布于 2024-08-07 11:12:38 字数 1216 浏览 2 评论 0原文

我将 JPanel 的背景颜色设置为白色。但是,当我将其保存为 JPG 或其他图像格式时,背景是黑色的。我已输入此代码 TYPE_INT_ARGB 但它不起作用。如何将背景设置为其他颜色?例如蓝色、白色等。

    public void paintComponent(Graphics g) {
       int width = getWidth();
       int height = getHeight();

       // Create a buffered image in which to draw
       BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

       // Create a graphics contents on the buffered image
       Graphics2D g2d = bufferedImage.createGraphics();     
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       g2d.setStroke(new BasicStroke(1)); // set the thickness of polygon line
       g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));
       g2d.setPaint(Color.black);//color of the polygon line
       g2d.setBackground(Color.WHITE);

       //draw polygon
       for (Polygon triangle : triangles)  
         g2d.drawPolygon(triangle);

       try {
           File file = new File("newimage.jpg");
           ImageIO.write(bufferedImage, "jpg", file);
       } catch (IOException e) {
         }          
 }//public void paint(Graphics g)   

I set a JPanel's background color to white. However, when I save it into JPG or another image format, the background is black. I have put this code TYPE_INT_ARGB but it doesn't work. How can I set the background to other color? e.g. blue, white etc.

    public void paintComponent(Graphics g) {
       int width = getWidth();
       int height = getHeight();

       // Create a buffered image in which to draw
       BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

       // Create a graphics contents on the buffered image
       Graphics2D g2d = bufferedImage.createGraphics();     
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       g2d.setStroke(new BasicStroke(1)); // set the thickness of polygon line
       g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));
       g2d.setPaint(Color.black);//color of the polygon line
       g2d.setBackground(Color.WHITE);

       //draw polygon
       for (Polygon triangle : triangles)  
         g2d.drawPolygon(triangle);

       try {
           File file = new File("newimage.jpg");
           ImageIO.write(bufferedImage, "jpg", file);
       } catch (IOException e) {
         }          
 }//public void paint(Graphics g)   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦萦几度 2024-08-14 11:12:38

我确实意识到这是一个非常古老的问题,但我自己也遇到了类似的问题,因为我找到了答案(在 javadoc 中),所以我想无论如何我都会将其发布在这里。

当您在图形对象上设置背景颜色时,只有在清除区域时才会发挥作用。因此,只需立即清除整个区域,您就会拥有您喜欢的颜色的背景:

graphics.clearRect(0, 0, width, height);

I do realize that this is a very old question but I had a similar issue myself and since I found the answer (it's in the javadocs) I thought I would post it here anyway.

When you set the background color on the graphics object this will only come into play when you clear a region. Hence, just clear the entire area right away and you will have the background in your preferred color:

graphics.clearRect(0, 0, width, height);
弥枳 2024-08-14 11:12:38

您在问题中给出解决方案。您将面板的背景设置为白色,而不是 BufferedImage。您将图像保存为 JPEG,而不是面板,因此 JPEG 具有默认背景,显示为黑色。

You give the solution in your question. You set the background of the panel to white, not the BufferedImage. You save the image as a JPEG, not the panel, so the JPEG has the default background, which shows up as black.

¢好甜 2024-08-14 11:12:38

保存为 JPEG 时您希望有什么背景? JPEG 适用于摄影,它不能有透明区域,因此必须将这些区域转换为某种颜色,这就是为什么你有黑色(我想)。为什么不将图像另存为 PNG 格式?或者,如果您想坚持使用 JPEG,首先用白色填充区域,然后开始在其上绘图...

what do you expect to have as a background when saving as JPEG? JPEG is meant for photographies, it cannot have transparent areas, so those must be converted to some color, that is why you have black (I suppose). Why don't you save image as PNG? Or if you want to stick with JPEG first fill area with white color, then start drawing onto it...

灼痛 2024-08-14 11:12:38

如果你问我的话,你创建图像的方法是倒退的。所有其他问题都是关于在面板上绘制多边形。现在您正在更改代码以在图像上绘画?

当您扩展 JPanel 并调用 super.paintComponent() 时,猜猜会发生什么?背景就画好了!然后您进行自定义多边形绘制。在上面的代码中,您只需创建图像,然后绘制多边形。

更简单的方法是仅创建一个将面板绘制到图像的例程,然后您可以重用代码,而无需重写每个组件的 PaintComponent 方法。

ScreenImage 类会为您完成此操作。

Your approach to creating the image is backwards if you ask me. All you other questions have been about painting polygons on a panel. Now you are changing the code to paint on an image?

When you extend JPanel and invoke super.paintComponent() guess what happens? The background gets painted! Then you do your custom polygon painting. In the above code you just create the image and then paint the polygons.

The easier approach is to just create a routine the paints the panel to the image, then you can reuse the code without overriding the paintComponent method of every component.

The ScreenImage class does this for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文