使用 Component.createImage(ImageProducer) 和自定义 ImageProducer 在 Java 中创建图像
我正在开发一个 Java 分形程序。在我的设计中,我想实现一个特定的 ImageProducer 类,该类在调用 startProduction(ImageConsumer)
时在后台工作线程中计算像素。设置生产者后,将使用 Component.createImage(ImageProducer) 创建图像,然后通过调用 Graphics.drawImage(Image, int, int, ImageObserver) 来显示图像在 JPanel 的相应子类的 paintComponent(Graphics)
方法中,传递 this
(JPanel)作为最后一个参数。
这里的问题是:从工作线程调用注册的 ImageConsumers 的 setter 方法是否安全?这些调用不应该来自 EDT 以便正确绘制 JPanel 吗?
I'm working on a Java fractal program. In my design, I would like to implement a particular ImageProducer class which computes pixels in a background worker thread when the startProduction(ImageConsumer)
gets invoked. Once set up the producer, image will be created with Component.createImage(ImageProducer)
and then shown with a call to Graphics.drawImage(Image, int, int, ImageObserver)
in the paintComponent(Graphics)
method of an appropriate subclass of JPanel, passing this
(the JPanel) as the last parameter.
Here the question: is it safe to call setter methods on registered ImageConsumers from the worker thread? Shouldn't these calls come from the EDT for the JPanel to be painted properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您必须同步 访问两个或多个线程共享的任何数据。这篇文章讨论了一些方法和权衡。此外,您可能会从此 获得一些关于颜色表动画的想法示例。最后,
Benojt
项目是有关该主题的丰富研究资源。In general, you must synchronize access to any data shared by two or more threads. This article discusses some of the approaches and tradeoffs. In addition, you may get some ideas for color table animation from this example. Finally, the
Benojt
project is a rich source of study on the topic.