我一直遇到RenderableImageProducer的问题,我认为这是由于多线程。基本上,如果我创建几个RenderableImageProducers引用相同的RenderableImage,它们似乎混淆了,好像它们正在共享一些状态。
有人可以看看并告诉我,我的代码是否犯了错误(很可能!)或者JavaSE库中是否存在错误?
我已经在下面发布了我的测试代码(通过剥离我的“真实”代码来实现),抱歉它有点长但我不能让它更短。
package imagemangler;
import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderContext;
import java.awt.image.renderable.RenderableImage;
import java.awt.image.renderable.RenderableImageProducer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyRenderableImage implements RenderableImage {
/* Implement RenderableImage methods */
public Vector<RenderableImage> getSources() {
return null;
}
public Object getProperty(String name) {
return java.awt.Image.UndefinedProperty;
}
public String[] getPropertyNames() {
return null;
}
public boolean isDynamic() {
return false;
}
public float getWidth() {
return 256.0f;
}
public float getHeight() {
return 256.0f;
}
public float getMinX() {
return 0.0f;
}
public float getMinY() {
return 0.0f;
}
public RenderedImage createDefaultRendering() {
int width = 256;
int height = 256;
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Creating render {0}x{1} on thread {2}", new Object[]{width, height, Thread.currentThread().getId()});
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Don't bother drawing anything
return bi;
}
/* Create eight scaled copies; RenderableImageProducer will start a
* thread for each
*/
public static void main(String[] args) {
// Create a dummy component.
Component component = new Component() {
};
Image base = component.createImage(new RenderableImageProducer(new MyRenderableImage(), null));
MediaTracker mt = new MediaTracker(component);
for (int i = 0; i < 8; ++i) {
int size = 1 << i;
Image mipmap = base.getScaledInstance(size, size, Image.SCALE_SMOOTH);
mt.addImage(mipmap, i);
}
try {
mt.waitForAll(1000);
} catch (InterruptedException ex) {
}
}
public RenderedImage createScaledRendering(int w, int h, RenderingHints hints) {
throw new UnsupportedOperationException("Not supported yet.");
}
public RenderedImage createRendering(RenderContext renderContext) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
这给了我错误:
Exception in thread "RenderableImageProducer Thread" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.java:404)
我已尽可能多地进行跟踪,但无法查看代码中的错误。
如果我为'base'使用不同的图像,main()函数可以正常工作,例如来自component.getImage(String)的东西,甚至是自定义的ImageProducer。
因此我认为它必须是RenderableImageProducer中的东西。
我使用的是java版“1.6.0_23” Java(TM)SE运行时环境(版本1.6.0_23-b05) Java HotSpot(TM)客户端VM(内置19.0-b09,混合模式,共享) 在WinXP上,SP3 32位