Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Solved]How can I draw an image that isn't bundled with my mod to a GUI?

Featured Replies

Posted

Hi, I'm having some troubles regarding drawing images. In my mod, I'm creating images at runtime that I want to be able to show in a gui screen. Essentially, I want to be able to load a texture either a) from an external image file directly (probably won't work) or b) via a BufferedImage object.

 

Due to the recent changes introduced with resource packs, it seems to be difficult to find any information about it and with the code almost entirely obfuscated, I've had troubles getting anywhere searching for a solution.

 

It's not valid any more, but what I'm looking for is basically an updated version of this solution: https://gist.github.com/thvortex/2138298

 

By scanning for references to BufferedImage, I found a few methods related to it in the TextureUtil class, but I don't know if they're what I'm looking for or how I should be using them.

 

I'm thankful for any help.

  • Author

Thanks! I got it working by writing my own renderer based on the MapItemRenderer.

 

For future refence, here's (a slightly modified version of) my code.

package panoramakit.gui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;

/** 
* Takes care of loading and drawing previews to the screen.
* @author dayanto
*/
public class PreviewRenderer
{
private File file;
private BufferedImage image;
private DynamicTexture previewTexture;
private ResourceLocation resourceLocation;
private TextureManager textureManager;

public PreviewRenderer(TextureManager textureManager, File file)
{
	this.textureManager = textureManager;
	this.file = file;
}

/**
 * Attempts to load the image. Returns whether it was successful or not.
 */
public boolean loadPreview()
{
	try {
		image = ImageIO.read(file);
		previewTexture = new DynamicTexture(image);
		resourceLocation = textureManager.getDynamicTextureLocation("preivew", previewTexture);
		return true;
	} catch (IOException e) {
		e.printStackTrace();
		return false;
	}
}

public void drawCenteredImage(int xPos, int yPos, int width, int height)
{
	if(previewTexture == null) {
		boolean successful = loadPreview();
		if(!successful) return;
	}
	drawImage(xPos + (width - image.getWidth()) / 2, yPos + (height - image.getHeight()) / 2, image.getWidth(), image.getHeight());
}

private void drawImage(int xPos, int yPos, int width, int height)
{
	previewTexture.updateDynamicTexture();
	Tessellator tessellator = Tessellator.instance;
	textureManager.bindTexture(resourceLocation);
	GL11.glEnable(GL11.GL_BLEND);
	GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
	GL11.glDisable(GL11.GL_ALPHA_TEST);
	tessellator.startDrawingQuads();
	tessellator.addVertexWithUV(xPos        , yPos + height, 0, 0.0, 1.0);
	tessellator.addVertexWithUV(xPos + width, yPos + height, 0, 1.0, 1.0);
	tessellator.addVertexWithUV(xPos + width, yPos         , 0, 1.0, 0.0);
	tessellator.addVertexWithUV(xPos        , yPos         , 0, 0.0, 0.0);
	tessellator.draw();
	GL11.glEnable(GL11.GL_ALPHA_TEST);
	GL11.glDisable(GL11.GL_BLEND);
}
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.