简体   繁体   English

如何有效地清除Java中的部分图形层?

[英]How can I effectively clear part of the Graphics layer in Java?

I'm developing a rendering engine using Swing. 我正在使用Swing开发渲染引擎。 Now I have run into a very odd problem. 现在我遇到了一个非常奇怪的问题。

Imagine we must draw something on our Graphics2D context, and then clear it. 想象一下,我们必须在Graphics2D上下文中绘制一些东西,然后清除它。 Should notice here that I'm drawing on a BufferedImages 's context, but it shouldn't matter, as paintComponent() method later simply outputs that image to the screen. 这里应该注意我正在绘制BufferedImages的上下文,但它应该没关系,因为paintComponent()方法稍后只是将该图像输出到屏幕。

So I should draw something, then apply some custom blurring code, then repaint my area partly (I'm making a box-shadow). 所以我应该绘制一些东西,然后应用一些自定义的模糊代码,然后重新绘制我的区域(我正在制作一个盒子阴影)。 And I need to use the alpha channel so that my layer must be able to be partly transparent. 我需要使用alpha通道,以便我的图层必须能够部分透明。

Now I am using this code for repainting a custom image area: 现在我使用此代码重新绘制自定义图像区域:

private void repaintRegion(Graphics2D g2d, int x, int y, int w, int h, BufferedImage img) {

    g2d.clearRect(x, y, w, h);
    composite = AlphaComposite.getInstance(AlphaComposite.SRC_OUT, 1f);
    g2d.setComposite(composite);
    g2d.drawImage(img, x, y, this);

    composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
    g2d.setComposite(composite);

    g2d.drawImage(img, x, y, this);
}

After that it is just fine. 之后就好了。 But if I remove painting in SRC_OUT mode, I get a black rectangle. 但是如果我在SRC_OUT模式下删除绘画,我会得到一个黑色矩形。

But this way it also works fine: 但这样它也可以正常工作:

private void repaintRegion(Graphics2D g2d, int x, int y, int w, int h, BufferedImage img) {

    AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.CLEAR, 1f);
    g2d.setComposite(composite);

    g2d.fillRect(x, y, w, h);

    composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f);
    g2d.setComposite(composite);

    g2d.drawImage(img, x, y, this);
}

Can someone explain me, why it is so, and what is happening when I am using clearRect()? 有人可以解释我,为什么会这样,以及当我使用clearRect()时发生了什么?

I use clearRect() in my applications and it works fine. 我在我的应用程序中使用clearRect() ,它工作正常。 However, I don't mess with the composite property of the context. 但是,我不会搞乱上下文的复合属性。

Keep in mind you also might need to make sure the background color is transparent (I believe the default is black) as clearRect() uses that as the fill color for what it clears (see https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#clearRect(int,%20int,%20int,%20int) ) 请记住,您还可能需要确保背景颜色是透明的(我相信默认为黑色),因为clearRect()将其用作清除它的填充颜色(请参阅https://docs.oracle.com/javase) /7/docs/api/java/awt/Graphics.html#clearRect(int,%20int,%20int,%20int) )

Also make sure that the BufferedImage you are drawing on has an alpha channel (use an image type with an alpha channel when calling the constructor) see https://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#field_summary for options. 还要确保您正在绘制的BufferedImage具有alpha通道(在调用构造函数时使用带有alpha通道的图像类型)请参阅https://docs.oracle.com/javase/7/docs/api/java/awt /image/BufferedImage.html#field_summary用于选项。

To be totally proper, you could use the GraphisEnvironment to construct the BufferedImage : 为了完全正确,您可以使用GraphisEnvironment来构造BufferedImage

BufferedImage bufferedImage = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration()
                .createCompatibleImage(w, h, BufferedImage.TRANSLUCENT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM