Hi.
having this tag
[xml] <image id="driverImage" width="100%" height="100%" filename="textures/driver83300.png" align="center" />[/xml]
I have to change dynamically image src using a BufferedImage
Is it possible?
You can change the image from java, however (to the extent of my knowledge) it has to be a nifty image. Look in the wiki.
rickard said:
You can change the image from java, however (to the extent of my knowledge) it has to be a nifty image. Look in the wiki.
I've looked everywhere, can you help me more?
It's not an image on filesystem but one dynamically created
You should look through the nifty image class and see if there’s a method to set the pixel data from the buffered image in runtime.
Solved,
just add my BufferedImage to AssetCache
[java]
private void setNiftyImage(BufferedImage b) {
TextureKey key =new TextureKey(“textures/Monkey.jpg”,false);
Texture tex = assetManager.loadTexture(key);
tex.setAnisotropicFilter(16);
tex.setMagFilter(MagFilter.Bilinear.Bilinear);
AWTLoader loader =new AWTLoader();
Image imageJME=null;
imageJME = loader.load(b, true);
tex.setImage(imageJME);
((DesktopAssetManager)assetManager).addToCache(new AssetKey(“pippo”), tex);
NiftyImage img = nifty.getRenderEngine().createImage(“pippo”, false);
driverImage.getRenderer(ImageRenderer.class).setImage(img);
}
[/java]
Just for the record, changing images from java has been added to the nifty wiki some time ago here is the description on how you do that in general
It does not cover how to use a BufferedImage but you’ve solved that already
Hello I just tried this piece of code but on my side it seems i have an exception when i execute this line:
[java]NiftyImage img = nifty.getRenderEngine().createImage(“pippo”, false);[/java]
the createImage(“pippo”, false) function replies => No loader registered for type “”
Do you have any ideas?
I have another question about this methode, can we use it to display a “real time” bufferedImage ?
On my side i need to display audio samples on my nifty gui application, and so the refresh rate is likely to be 1 image update every 20-100ms.
Is there another way to load buffered image without creating a texture first?
the createImage(“pippo”, false) function replies => No loader registered for type “”
yes, that makes sense! you just need to read the error message, it's all there ;)
createImage wants to read an actual file and I think the JME implementation uses the file extension (in your case empty ^^) to determine the file type tne the correct loader to load the image (for instance ".png" => PNGLoader, ".tga" => TGA Loader and so on).
I can't help with the dynamic image update question with a bufferedImage, I'm afraid. maybe there is some direct texture access workaround possible using some jme specific code?
Thanks for the reply. I’ll try to find another way combining Nifty and JME HUD.
Or even create a Ramdisk to write and read generated images. But i’d like to avoid that, it’s just a loss of resources.
i updated my code to this:
[java]private void LoadVolumeResource(String sResource) {
TextureKey key = new TextureKey(sResource,false);
Texture tex = m_Context.m_App.getAssetManager().loadTexture(key);
tex.setAnisotropicFilter(16);
tex.setMagFilter(MagFilter.Bilinear.Bilinear);
AWTLoader loader =new AWTLoader();
Image imageJME=null;
BufferedImage img = null;
try {
img = ImageIO.read(new File(“Assets/” + sResource));
} catch (IOException e) {
}
imageJME = loader.load(img, true);
tex.setImage(imageJME);
m_Context.m_App.getAssetManager().registerLoader(“PNGLoader”, “.png”);
((DesktopAssetManager)m_Context.m_App.getAssetManager()).addToCache(new AssetKey(“pippo.png”), tex);
NiftyImage img2 = null;
try{
img2 = m_nifty.getRenderEngine().createImage(“pippo.png”, false);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
m_VolumeBar.getRenderer(ImageRenderer.class).setImage(img2);
}[/java]
Now when doing [java]img2 = m_nifty.getRenderEngine().createImage(“pippo.png”, false);[/java] i have the exception :
pippo.png (Flipped)
but i have registered the loader : m_Context.m_App.getAssetManager().registerLoader(“PNGLoader”, “.png”);
and added the “.png” extension to the assek key name. (if i have the same exception as before)
my input resouce is a png (called “Textures/VolumeInner.png”)
Do you have any ideas?
the exception is missing in your post? o_O
lol no indeed the exception is “pippo.png (Flipped)” <=
I also tried to copy the loader and remove the part where the image is flipped but i still have the same exception.
I don’t understand what the heck is going on …
are you sure you have the path correct setting just “pippo.png” wouldn’t that mean its looking for pippo.png in the folder assets/
and I don’t understand this line .getAssetManager().registerLoader(“PNGLoader”, “.png”); shouldn’t it be something like PNGLoader.class or PNGLoader.class.getName() it looks odd but i haven’t had to do it since jme3 was moved to trunk I think all of the necessarily loaders are created from default. I am not sure though.
lol i am not sure what is going on though XD
Indeed AWTloader is doing the job with Png files and so registering the loader is not necessary.
However to summarise:
java.addToCache(new AssetKey(“pippo”), tex);
NiftyImage img = nifty.getRenderEngine().createImage(“pippo”, false);
driverImage.getRenderer(ImageRenderer.class).setImage(img);[/java]
This statement is throwing exception : “No loader registered for type”
And settings java.addToCache(new AssetKey(“pippo.png”), tex);
NiftyImage img = nifty.getRenderEngine().createImage(“pippo.png”, false);[/java]
is throwing the other exception : “pippo.png (Flipped)”.
As my first goal is to be able to create a NiftyImage from a BufferedImage, maybe there is another way to do so?
(i’m generating an image using Java2D as a BufferedImage and i want to display this image on Nifty gui)
Can you try:
[java]
((DesktopAssetManager)assetManager).addToCache(new TextureKey("pippo.png"), tex);
[/java]
instead of AssetKey? That has been working for me for quite a while.
I do the exact same procedure (generate a bufferedimage i use in nifty=
Fine I’ll give it a try!
Ok i gave it a try but i have the same troubles => Assets cannot be found.
Anyway i give up on this one. Plus i’m using the nightly build of 03-29-2011 so that’s may be why it’s not working.
As i can’t use the lasted nightly build due to major changes on the spider monkey API (and documentation is partially up to date). At least it was the case when i tried nightly build 05-20-2011.
I guess i’ll try nightly build of today and if my network part is too difficult to update i’ll do something else.
Just wanted to inform you that I got this one running.
I created a BufferedImage in Java code and succeeded in putting it into a nifty image element with following code copied from above in ths thread (mention the code change “new AssetKey(“pippo”)” to “new TextureKey(“pippo”)”, this made the difference for me, thanks rickard!):
[java]
TextureKey textureKey = new TextureKey(“Textures/Monkey.jpg”, false);
Texture tex = app.getAssetManager().loadTexture(textureKey);
tex.setAnisotropicFilter(16);
tex.setMagFilter(MagFilter.Bilinear);
AWTLoader loader = new AWTLoader();
Image imageJME = null;
imageJME = loader.load(bufferedImage, true);
tex.setImage(imageJME);
((DesktopAssetManager) app.getAssetManager()).addToCache(new TextureKey(“pippo”), tex);
[/java]
I create a new image layer with this bufferedImage:
[java] new ImageBuilder(“test”) {{
x(“100px”);
y(“100px”);
width(“150px”);
height(“100px”);
filename(“pippo”);
}}.build(nifty, startScreen, myLayer);
[/java]
I would appreciate if there would be a smarter way creating a image from bufferedimage, maybe like this? :
[java] new ImageBuilder(“test”) {{
x(“100px”);
y(“100px”);
width(“150px”);
height(“100px”);
image(bufferedImage);
}}.build(nifty, startScreen, myLayer);
[/java]
Just a proposal…
Thanks to all!