我目前正在分析我的Java-2D应用程序(用于学习的游戏引擎)。由于我不能保证每个帧都被完全覆盖,所以我必须将每个帧的背景清除为纯色(即Color.BLACK)。
我这样做的方式很慢(在我的环境中,大约40%的绘图时间都花在了清除背景上)。
首先,我从bufferStrategy获取一个图形上下文,然后在上面绘制一个全分辨率的PickYourColor矩形,然后再绘制实际内容。
// 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,但它非常简短且不言自明)
/**
* 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());
}发布于 2015-03-10 04:46:43
我不能说为什么fillRect很慢,但你可以试着创建一个Image并将其绘制为bg。虽然不确定它是否会更快。
尝试:
BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
int[] imageData =((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
Arrays.fill(imageData, 0);然后绘制Image,而不是fillRect
graphics.drawImage(bi, 0, 0, null);告诉我进展如何(我对此有疑问)。
发布于 2015-03-18 06:24:05
如果您想要清除整个背景,请尝试canvas.drawColor(color, PorterDuff.Mode.CLEAR)。应该会快一点
https://stackoverflow.com/questions/28950423
复制相似问题