在 Java 中绘制静态图像的最快方法
我正在编写自定义热图生成器。我想知道用 Java 绘制方框(最多约 100 万个)最快的方法是什么。我发现的大多数问题都集中在动态图像(例如游戏中),我想知道是否有更好的方法来处理静态图像。我尝试过使用 swing(通过 GridLayout 并向每个框添加彩色画布)、使用 Graphics2D 直接在面板上绘图以及使用处理库。虽然处理速度相当快并生成干净的图像,但窗口似乎在保持图像方面存在问题;每当您最小化、移动窗口等时,它都会生成图像的不同部分。
我听说过 OpenGL,但我从未接触过它,并且我想要一些关于它(或其他东西)是否会更好的反馈在投入时间之前先采取方法。
I'm in the process of writing a custom heatmap generator. I'm wondering what the fastest way is to draw boxes (up to around 1 million) in Java. Most questions I've found have concentrated on dynamic images (like in games), and I'm wondering if there's a better way to go for static images. I've tried using swing (via a GridLayout and adding a colored canvas to each box), drawing directly on the panel with Graphics2D, and also by using the Processing libraries. While Processing is pretty fast and generates a clean image, the window seems to have problems keeping it; it generates different parts of the image whenever you minimize, move the windows, etc.
I've heard of OpenGL, but I've never touched it, and I wanted some feedback as to whether that (or something else) would be a better approach before investing time in it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于静态图像,我将它们绘制到 BufferedImage (BI),然后通过 Graphics2D 进行绘制。
我保留一个布尔值来告诉我 BI 是否是最新的。这样我只需要支付一次昂贵的油漆费用。如果您想变得更奇特,您可以缩放 BI 以处理较小的大小调整。对于重大调整大小,您可能需要重新绘制 BI,以免引入伪影。它对于覆盖数据(例如十字线、光标下的值等)也很有用,因为您只绘制 BI 和数据。
For static images, I paint them to a BufferedImage (BI) and then draw that via Graphics2D.
I keep a boolean that tells me whether the BI is up to date. That way I only incur the expensive painting cost once. If you want to get fancy, you can scale the BI to handle minor resizing. For a major resizing you'll probably want to repaint the BI so as not to introduce artifacts. It's also useful for overlaying data (such as cross hairs, the value under the cursor, etc) as you're only painting the BI and the data.