Image and Image

Is there a connection between com.jme.image.Image and java.awt.Image?



I ask this because I want to get a java.awt.Image from a graphicsImage. The reason I want a java.awt.Image (actually I need a BufferedImage) is that I want to save it.

take a look at TextureManager.loadImage(java.awt.Image image, boolean flipImage)



there you can see how the conversion from java.awt.Image to com.jme.image.Image is done. I suppose you'll figure then out how to convert back to java.awt.Image :wink:

hmm…i need the same thing. i tried, but it didn't work. does anyony have a solution?

what exactly didn't work?



an example or a code sample would be very helpful.

? maybe look to see how simpleGameScreenShot is done ??

hi,



i can't get the byte array with the correct image information:



Image img = ts.getTexture(0).getImage();

byte[] byteA = new byte[4 * img.getWidth() * img.getHeight()];

int count = 0;
while(img.getData().remaining() > 0) {
            byteA[count] = img.getData().get();

hobi said:

after that i construct a buffered image from the information and display it in a swing component. but every byte in the array is 0, so that nochting is displayed. when i manually set the bytes in the while loop to a value i can see the rectangle.

...
when i create a TextureState with a texture made from "img", and set it to a quad, no texture is displayed there, too.



there are a few possibilities:
-you image is broken(which i assume is not the case) or it fails to load. if indeed you apply the texture state (your ts variable, right after successfully loading it) to let's say a quad and no texture is displayed, then the actual problem probably happens when loading the image.
-your while loop terminates immediately (does count ever get bigger than 0?), that would mean the buffer's current position is at the end of the buffer. you could try to either rewind() the buffer or do something like img.getData().asReadOnlyBuffer() and then rewind() the read-only buffer(if you don't want to alter the original buffer). you could also use the buffer.get(byteArray[]) method for filling your array instead of that while loop.

when i use the TextureState (from which i extract the texture and image for the converting) on another quad, everything is displayed correctly. the TextureState is from a JMEDesktop. but as i said before, when i want to use the image from that TaxtureState/Texture with

Image img = ts.getTexture(0).getImage();

on a second TextureState/Texture which i apply to a quad, i can't see anything on the quad. so i guess that i'm doing something wrong at this point where i extract the image from the original TextureState. But it's curious that methods like getWidth and getHeight return the correct sizes from the img object(1024x768).



i tried your tips with the buffer, but unfortunately it didn't work. The while loop runs correctly to the end (the count variable ends at 3145728 = 1024x768x4).

hmm…i just did the same with a simple quad. getting the image of the quad's texture is no problem. is there something special about the JMEDesktop? do i have to get the texture image from a JMEDesktop in a different way?