Java - 使用 Graphics.drawImage() 和 2 屏幕缓冲策略绘制许多图像会扭曲和剪切图像
我正在使用循环来调用双缓冲绘画。这与覆盖我唯一的面板的重绘方法一起,旨在将重绘的完全控制传递给我的循环,并且仅在必要时才渲染(即在 GUI 中进行了一些更改)。
这是我的渲染例程:
Log.write("renderer painting");
setNeedsRendering(false);
Graphics g = frame.getBufferStrategy().getDrawGraphics();
g.setFont(font);
g.setColor(Color.BLACK);
g.fillRect(0, 0, window.getWidth(),window.getHeight());
if(frame != null)
window.paint(g);
g.dispose();
frame.getBufferStrategy().show();
如您所见,它非常标准。我从缓冲区策略中获取 grpahics 对象(初始化为 2),将其设置为黑色并将其传递给“窗口”对象的 Paint 方法。
使用图形对象完成窗口后,我将其处理并调用缓冲区策略上的 show 来显示虚拟缓冲区的内容。
需要注意的是,窗口将图形对象传递给填充窗口的许多其他子组件,而每个子组件又使用图形对象的相同实例在屏幕上绘制某些内容:文本、形状或图像。
当系统运行并渲染大图像时,我的问题开始出现。图像似乎被切割成七块,并在图像应渲染的位置内以不同的偏移量一次又一次地绘制(3-4 次)。请参阅我的附图:
这是原始图像: 替代文本 http://img109.imageshack.us/img109/8308/controller.png< /a>
这是我得到的: 替代文本 http://img258.imageshack.us/img258/3248/probv.png< /a>
请注意,在第二张图片中,我在图片上渲染形状 - 这些形状始终位于正确的位置。
知道为什么会发生这种情况吗? 如果我将图像保存到文件中,就像它在内存中一样,就在调用 g.drawImage(...) 之前,它与原始图像相同。
I am using a loop to invoke double buffering painting. This, together with overriding my only Panel's repaint method, is designed to pass complete control of repaint to my loop and only render when it necessary (i.e. some change was made in the GUI).
This is my rendering routine:
Log.write("renderer painting");
setNeedsRendering(false);
Graphics g = frame.getBufferStrategy().getDrawGraphics();
g.setFont(font);
g.setColor(Color.BLACK);
g.fillRect(0, 0, window.getWidth(),window.getHeight());
if(frame != null)
window.paint(g);
g.dispose();
frame.getBufferStrategy().show();
As you can see, it is pretty standard. I get the grpahics object from the buffer strategy (initialized to 2), make it all black and pass it to the paint method of my "window" object.
After window is done using the graphics object, I dispose of it and invoke show on the buffer strategy to display the contents of the virtual buffer.
It is important to note that window passes the graphics object to many other children components the populate the window and each one, in turn, uses the same instance of the graphics object to draw something onto the screen: text, shapes, or images.
My problem begins to show when the system is running and a large image is rendered. The image appears to be cut into seveeal pieces and drawn again and again (3-4 times) with different offsets inside of where the image is supposed to be rendered. See my attached images:
This is the original image:
alt text http://img109.imageshack.us/img109/8308/controller.png
This is what I get:
alt text http://img258.imageshack.us/img258/3248/probv.png
Note that in the second picture, I am rendering shapes over the picture - these are always at the correct position.
Any idea why this is happening?
If I save the image to file, as it is in memory, right before the call to g.drawImage(...) it is identical to the original.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃,你正在使用 Swing 吗?
通常,Swing 自动渲染图像,您无法将其关闭。重绘()
方法越界是因为 Swing 有一个非常复杂的渲染例程
AWT 小部件的方法兼容性和多项优化,包括 仅绘图
必要时!
如果您想使用高速绘图 API,请使用带有 BufferStrategy 的组件
像 JFrame 和 Window 一样,使用
setIgnoreRepaint(false);
要关闭 Swing 渲染,请设置绘图循环并绘制内容本身。
或者您可以使用 JOGL 进行 OpenGL 渲染。你使用的方法似乎完全
与正确的 Java2D 用法不一致。
这里正确的使用:
Uh, you are using Swing ?
Normally Swing automatically renders the image, you can't switch it off. The repaint()
method is out of bounds because Swing has a very complicated rendering routine due to
method compatibility for AWT widgets and several optimizations, inclusive drawing only
when necessary !
If you want to use the High-Speed Drawing API, you use a component with a BufferStrategy
like JFrame and Window, use
setIgnoreRepaint(false);
to switch off Swing rendering, set up a drawing loop and paint the content itself.
Or you can use JOGL for OpenGL rendering. The method you are using seems completely
at odds with correct Java2D usage.
Here the correct use: