Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:47:45+00:00 2026-06-04T21:47:45+00:00

Is it possible to load PNG Textures and draw Strings in LWJGL WITHOUT using

  • 0

Is it possible to load PNG Textures and draw Strings in LWJGL WITHOUT using the Slick Framework?

Everytime I google “how to load png images in lwjgl” I get answers like this -> “hey just use the textureloader from the slick framework”.
Same for “how to draw strings in lwjgl” -> “just use the TTFFont Class from the slick framework”

But I don’t want to use this half-way-crossframework design. Because I don’t think that this is the best way.

Are there any Libraries or Extensions for LWJGL that are only made for Textures or Strings?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:47:47+00:00Added an answer on June 4, 2026 at 9:47 pm

    Basically, you take a BufferedImage, use getRGB() to get the RGB of each pixel, take that data and put it into a ByteBuffer (the data type used to input image data to OpenGL), set some texture data, and create the GL_TEXTURE_2D.

    This code by Krythic does it:

    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    
    import javax.imageio.ImageIO;
    
    import org.lwjgl.BufferUtils;
    import org.lwjgl.opengl.GL12;
    
    import static org.lwjgl.opengl.GL11.*;
    
    public class TextureLoader {
        private static final int BYTES_PER_PIXEL = 4;//3 for RGB, 4 for RGBA
           public static int loadTexture(BufferedImage image){
    
              int[] pixels = new int[image.getWidth() * image.getHeight()];
                image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
    
                ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
    
                for(int y = 0; y < image.getHeight(); y++){
                    for(int x = 0; x < image.getWidth(); x++){
                        int pixel = pixels[y * image.getWidth() + x];
                        buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
                        buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
                        buffer.put((byte) (pixel & 0xFF));               // Blue component
                        buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
                    }
                }
    
                buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
    
                // You now have a ByteBuffer filled with the color data of each pixel.
                // Now just create a texture ID and bind it. Then you can load it using 
                // whatever OpenGL method you want, for example:
    
              int textureID = glGenTextures(); //Generate texture ID
                glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
    
                //Setup wrap mode
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
    
                //Setup texture scaling filtering
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
                //Send texel data to OpenGL
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    
                //Return the texture ID so we can bind it later again
              return textureID;
           }
    
           public static BufferedImage loadImage(String loc)
           {
                try {
                   return ImageIO.read(MainClass.class.getResource(loc));
                } catch (IOException e) {
                    //Error Handling Here
                }
               return null;
           }
    }
    

    To use this code, do something like this:

    BufferedImage image = TextureLoader.loadImage("/res/test.png");//The path is inside the jar file
    int textureID = TextureLoader.loadTexture(image);
    

    You can either save the textureID as a final variable(if the texture never changes), or unload the texture after each render using GL11.glDeleteTextures(textureID);

    To do text, just create a BufferedImage manually, and use createGraphics() to get a graphics2D() instance for the image. Then, use drawString() to draw onto the BufferedImage, load it into the TextureLoader, render it onscreen, and unload the texture using the method above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to load an html document using ajax and then perform a
Is it possible to load a single plugin from outside the cake root using
I am trying to load an 8713px wide PNG in Raphael and I get
Is it possible to load a view on a background thread? I am using
Is it possible to load a image using: <img src=image.php?image_id=1> ? for image.php :
Is it possible to load a referenced assembly only if the .NET Framework version
Possible Duplicate: Load local HTML file in a C# WebBrowser I´m creating program in
It's possible to load and parse DTD schema via javascript or jQuery ? I
Is it possible to load *.tga file in picturebox? Actually i need to create
Is it possible to load a URL in a WebView and resize it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.