首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速清除背景的方法

快速清除背景的方法
EN

Stack Overflow用户
提问于 2015-03-10 03:39:47
回答 2查看 822关注 0票数 0

我目前正在分析我的Java-2D应用程序(用于学习的游戏引擎)。由于我不能保证每个帧都被完全覆盖,所以我必须将每个帧的背景清除为纯色(即Color.BLACK)。

我这样做的方式很慢(在我的环境中,大约40%的绘图时间都花在了清除背景上)。

首先,我从bufferStrategy获取一个图形上下文,然后在上面绘制一个全分辨率的PickYourColor矩形,然后再绘制实际内容。

代码语言:javascript
复制
// fill background with solid color
graphics.setColor(Color.BLACK);
graphics.fillRect(
                0,
                0,
                (int) bounds.getWidth(),
                (int) bounds.getHeight());

有没有一种更有效的、与平台无关的方法来使用Java-2D将每个帧的背景清除为纯色(这不是LWJGL的问题)?

我正在寻找的是一个graphics.clearBackgroundToSolidColor(颜色颜色)-方法...

按要求:这里是完整的呈现方法(它不是一个SSCCE,但它非常简短且不言自明)

代码语言:javascript
复制
/**
 * Create a new graphics context to draw on and
 * notify all RenderListeners about rendering.
 */
public void render() {

    ///// abort drawing if we don't have focus /////
    if (!this.windowJFrame.hasFocus()) {
        return;
    }


    ///// draw and create new graphics context /////
    Graphics2D graphics = null;
    do {

        try {
            graphics = (Graphics2D) this.bufferStrategy.getDrawGraphics();
            Rectangle2 bounds = this.getBounds();

            // set an inexpensive, yet pretty nice looking, rendering directives
            graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);



            // fill background with solid color
            graphics.setColor(Color.BLACK);
            graphics.fillRect(
                    0,
                    0,
                    (int) bounds.getWidth(),
                    (int) bounds.getHeight());


            // notify all listeners that they can draw now
            synchronized (this.renderListeners) {
                for (RenderInterface r : this.renderListeners) {
                    r.render(graphics, bounds);
                }
            }

            // show buffer
            graphics.dispose();
            this.bufferStrategy.show();


        } catch (Exception e) {

            Logger.saveMessage("window", Logger.WARNING, "Caught exception while drawing frame. Exception: " + e.toString());
        }

    } while (this.bufferStrategy.contentsLost());

}
EN

回答 2

Stack Overflow用户

发布于 2015-03-10 04:46:43

我不能说为什么fillRect很慢,但你可以试着创建一个Image并将其绘制为bg。虽然不确定它是否会更快。

尝试:

代码语言:javascript
复制
BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
int[] imageData =((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
Arrays.fill(imageData, 0);

然后绘制Image,而不是fillRect

代码语言:javascript
复制
graphics.drawImage(bi, 0, 0, null);

告诉我进展如何(我对此有疑问)。

票数 0
EN

Stack Overflow用户

发布于 2015-03-18 06:24:05

如果您想要清除整个背景,请尝试canvas.drawColor(color, PorterDuff.Mode.CLEAR)。应该会快一点

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28950423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档