Asset to File

Hi everybody.
I need to load a BufferedImage, then to convert it into texture.
I’ve written this small piece of code and it works:

[java]
galaxyImg = ImageIO.read(new File(“C:/cmazzuco/Tau Ceti/media/galaxy.png”));
galaxyImg.createGraphics();

    AWTLoader al = new AWTLoader();
    Image i = al.load(galaxyImg, false);

    Texture2D t2d = new Texture2D(i);

[/java]

but i’d like to use a more polished Asset path like “Textures/galaxy.png”, the absolute path is the evil.
How can i create a File instance starting from i.e. “Textures/galaxy.png” ?

Why would you do it in such a complicated way anyway? Just use the AssetManager?
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_asset

I’ll try to use it.
I need to pass through the BufferedImage because i’ve to read (and write) the color of a pixel (x,y) of the image and with BufferedImage i use the .getRGB(x, y) method.
So i need something that load from asset to BufferedImage,

Thank you

Use ImageRaster or the ImagePainter plugin, then you don’t waste space by moving the data around in two realms all the time.

1 Like

Uhm… ok… pheraps there is also a better way.

I’ll try to explain my situation.
I want to draw a galaxy map and resize it.
This map is mixed bitmap and procedural.
I mean: when i zoom in, twice or three time i want only to resize the image using a classical scale algorithm. But when you increase the magnify factor, i want to replace the single pixel with a set of random point: the number of the “stars” will depends from the value of the color: the more bright is the pixel, the more stars are inside.
For a visual demonstration please look at this link: the text is in italian but you can see only the images:

Galaxy Map

To obtain this, in a normal java jPanel, i’ve used this code:

[java]
public void drawGalaxy(Graphics2D g2d) {
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
int step = 8;
int maxx = galaxyImage.getWidth() / step;
int maxy = galaxyImage.getHeight() / step;
int startx = (galaxyImage.getWidth() - maxx) / 3;
int starty = (galaxyImage.getHeight() - maxy) / 3;
int yy = 0;
Random random = new Random();
int tot=0;
for (int x = startx; x < startx + maxx; x++) {
int xx = 0;
for (int y = starty; y < starty + maxy; y++) {
int p = galaxyImage.getRGB(x, y);

            Color c = new Color(p);

            if (step &gt;= 8) {


                int lum = c.getRed() / 8;

                g2d.setColor(c);
                for (int i = 0; i &lt; lum; i++) {
                    tot=tot+lum;
                    int x2 = random.nextInt() % step;
                    int y2 = random.nextInt() % step;

                    g2d.drawLine(x2 + xx * step, y2 + yy * step, x2 + xx * step, y2 + yy * step);

                }
            } else {
                g2d.setColor(c);
                g2d.fill(new Rectangle2D.Double(xx * step, yy * step, step, step));
                //g2d.fillRect(xx * step, yy * step, step, step);
            }
            xx++;


        }
        yy++;

    }
}

[/java]

Now i need to implement it inside jMonkey. The map is a com.jme3.ui.Picture, and i think to use it to display on the screen attaching as a “gui node”.
But i’m just starting to use jmonkey, pheraps there’s a more brilliant way…

@pidigi said: But i'm just starting to use jmonkey, pheraps there's a more brilliant way..
@normen said: Use ImageRaster or the ImagePainter plugin, then you don't waste space by moving the data around in two realms all the time.

Ok, i’ve made some progress to do my stuff, but i’m a bit confused. Please apologize me, i’m starting with this beautiful engine and i’ve to learn a lot :slight_smile:

To display my map i’m using the Picture class that seems perfect to display 2d images inside a 3d world.

So:

[java]
galaxyTexture = assetManager.loadTexture(“Textures/galaxy.png”);
galaxyImg = galaxyTexture.getImage();
galaxyPic = new Picture(“Galaxy”);
galaxyPic.setTexture(assetManager, (Texture2D) galaxyTexture, true);
galaxyPic.setWidth(settings.getWidth());
galaxyPic.setHeight(settings.getHeight());
galaxyPic.setPosition(0, 0);
[/java]

then, to resize the map:

[java]
private Image resize(Image fromImg, int size) {
Format newFormat = fromImg.getFormat();
int width = fromImg.getWidth();
int height = fromImg.getHeight();
ByteBuffer data = BufferUtils.createByteBuffer((int) Math.ceil(newFormat.getBitsPerPixel() / 8.0) * width * height);
Image toImage = new Image(Format.ABGR8, fromImg.getWidth(), fromImg.getHeight(), data);

    ImageRaster fromImgRaster = ImageRaster.create(fromImg);
    ImageRaster toImgRaster = ImageRaster.create(toImage);

[…]

    ColorRGBA color = fromImgRaster.getPixel(x, y);

[…]
toImgRaster.setPixel(xx * size, yy * size, color);

[…]

    return toImage;
}

[/java]

this because i need to work with the pixels of the image so i need ImageRaster, so i need Image.

Well now i’ve the Image with the resized map, now i want to upgrade the Picture that is displayed on the screen.
But i’m not able to find how to pass from Image to Picture. The only method that seems interesting is setTexture of Picture, but i cannot find a way to obtain a “Texture2d” class from Image.
Also looking on ImagePainter library.

What am I doing wrong?

Really the javadocs are only one click away in the SDK… and this was two clicks away from the forum:
http://hub.jmonkeyengine.org/javadoc/com/jme3/texture/Texture2D.html#Texture2D(com.jme3.texture.Image)

Sorry: i’ve tried to look inside the javadocs, but of Texture class, and i’ve found nothing. I haven’t look at Texture2D: i’m still a bit confused about the differences of the classes.
Anyway thank you: now it works.