Hi,
I have the following class which works perfect on my desktop but causes a NPE when running on my android device:
[java]
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import com.jme3.texture.Texture2D;
import com.jme3.texture.image.ImageRaster;
import java.nio.ByteBuffer;
import java.util.List;
/**
-
You can draw on this texture based on a given template.
**/
public class TemplatedTexture extends Texture2D {private Texture2D template;
private ImageRaster templateRaster;
private ImageRaster imageRaster;public TemplatedTexture(Texture2D template) {
super();
this.template = template;
Image image = buildFromTemplate(template);
setImage(image);
imageRaster = ImageRaster.create(image);
templateRaster = ImageRaster.create(template.getImage());
}private Image buildFromTemplate(Texture2D template) {
Image imageTemplate = template.getImage();
Image image = new Image();
image.setWidth(imageTemplate.getWidth());
image.setHeight(imageTemplate.getHeight());
image.setFormat(imageTemplate.getFormat());
image.setMultiSamples(imageTemplate.getMultiSamples());
image.setMipMapSizes(imageTemplate.getMipMapSizes());
List<ByteBuffer> data = imageTemplate.getData();
for (int i = 0; i < data.size(); i++) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.get(i).capacity());
image.setData(i, byteBuffer);
}
return image;
}/**
- Fills all pixels with the given color where templateColor is set.
- @param templateColor color of the template where we want to draw something
- @param color the color to draw with
-
@param templateToleration toleration 0f-1f
*/
public void fill(ColorRGBA template, ColorRGBA color, float templateToleration) {
for (int y = 0; y < getImage().getHeight(); y++) {
for (int x = 0; x < getImage().getWidth(); x++) {
ColorRGBA pixel = templateRaster.getPixel(x, y);
if (tolerateEquals(pixel, template, templateToleration)) {
imageRaster.setPixel(x, y, color);
}
}
}
}
/**
- Checks if both colors are equal in a given toleration range.
- @param a
- @param b
- @param toleration
-
@return
*/
public boolean tolerateEquals(ColorRGBA a, ColorRGBA b, float toleration) {
if (FastMath.abs(a.getRed() - b.getRed()) > toleration) {
return false;
}
if (FastMath.abs(a.getGreen() - b.getGreen()) > toleration) {
return false;
}
if (FastMath.abs(a.getBlue() - b.getBlue()) > toleration) {
return false;
}
if (FastMath.abs(a.getAlpha() - b.getAlpha()) > toleration) {
return false;
}
return true;
}
}
[/java]
And the NPE looks like:
[java]
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: java.lang.NullPointerException
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: at com.jme3.texture.image.ByteOffsetImageCodec.writeComponents(ByteOffsetImageCodec.java:85)
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: at com.jme3.texture.image.DefaultImageRaster.setPixel(DefaultImageRaster.java:111)
08-25 22:22:39.304 31187 31222 E com.jme3.app.AndroidHarness: at mytest.TemplatedTexture.fill(TemplatedTexture.java:67)
[/java]
The ByteBuffer is null and I have no clue why that happen. I found this thread: http://hub.jmonkeyengine.org/forum/topic/imagepainter-npe-issue/ but there where no real solution to the problem.
Any suggestions why this is not working on android and how I can resolve this?