[SOLVED] Drawing Just A Portion Of An Image

I’m struggling doing a rather simple task :slight_smile: - I want to draw just a portion of a given image and not the entire image. I’m using this code:

Texture2D tex = (Texture2D)assetManager.loadTexture(resource.path);
 pic = new Picture(cmd.resourceName+"_"+_drawChannels.size());
 pic.setTexture(assetManager, tex, true);
// pic.setWidth(settings.getWidth()/2);
// pic.setHeight(settings.getHeight()/2);


And I’m ;looking for methods either in the Texture2D or in the Picture for selecting the desired portion of the image something like getting the : X,Y,Width,Height of the raw image.

Must be a way…

Well, here is what we do in similar situations, in case it gives you some inspiration:

1 Like

You probably want to create a quad with some different texture coordinates than just the four corners of the image.

Check out @ndebruyn’s galago framework, most importantly the sprite class:

Galago2D/Sprite.java at master · nickidebruyn/Galago2D · GitHub

1 Like

Thanks! I have updated the code and it works fine except that I don’t have transparency in the rendered image. here is the code:

    public void channelDraw(ChannelDrawCommand cmd) {

        Picture pic = _drawChannels.get(cmd.resourceName);
        String fromRes=cmd.resourceName;
        ResourceSetup2D resource = assetsMapping.getSpriteSheetsIndex().get(fromRes.toLowerCase());
            return ;

        if(pic==null) {

            BufferedImage img = null;

            try {
                img = ImageIO.read(assetManager.locateAsset(new AssetKey(resource.path)).openStream());
            } catch (IOException ex) {
                //LOGGER.log(Level.SEVERE, "Failed to load the backdrop image with " + image + "!", ex);

            int width = img.getWidth()/resource.cols;
            int height = img.getHeight()/resource.rows;
            int startX = 0;
            int startY=0;

            BufferedImage newImage = new BufferedImage(width, height, img != null ? img.getType() : BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = newImage.createGraphics();
            g.drawImage(img,0,0,width,height,startX,startY,startX+width,startY+height, Color.MAGENTA,null);

            AWTLoader loader = new AWTLoader();
            Texture2D tex = new Texture2D(loader.load(newImage, true));

            pic = new Picture(cmd.resourceName+"_"+_drawChannels.size());

            pic.setTexture(assetManager, tex, true);

        } else {
            pic.setImage(assetManager, resource.path, true);

        pic.setPosition(cmd.posXVal, settings.getHeight() - pic.getLocalScale().getY() - cmd.posYVal);


What is missing in order to make it draw with transparency?

OK, found it. needed to put null in the bgcolor:

g.drawImage(img,0,0,width,height,startX,startY,startX+width,startY+height, null,null);

Now it’s finally working as expected

If you ever decide that you don’t want to make a whole separate image + texture just to render part of a texture you’ve already loaded… then the texture coordinate answer is the right one and only takes one line of code.


I see future potential in the Graphics2D drawing methods so for now I’ll keep it

Yep, if you don’t need to modify the texture in any way, just shift it, the texture coordinates are probably way faster.

In our use case as well, using some shader trick to draw the texture partly (the arc thing) would have been much better and faster. But since I can’t yet handle those, Graphics2D is an hacky option.

1 Like