I have the problem that when I work in Eclipse I can get a window to open as it should but for some reason I can’t get it to start once I’ve exported the program to a JAR-file. I made a very simple example where I only draw a single image on the center of the screen and tried that. It worked in Eclipse and it didn’t work once exported (in a way I’m glad that is so, it shows the problem is quite basic and not a huge error in my other program).
I’ve added in the code here so that you can see. It works in Eclipse, but not when exported.
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.ARBDebugOutput.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ARBDebugOutputCallback;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.PixelFormat;
public class OpenGL_Example {
public static void main(String[] args) {
try {
//Display.setDisplayMode(new DisplayMode(600, 400));
Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
Display.create(new PixelFormat(), new ContextAttribs().withDebug(true));
//Display.setVSyncEnabled(true);
if(GLContext.getCapabilities().GL_ARB_debug_output){
glDebugMessageCallbackARB(new ARBDebugOutputCallback());
}
} catch (LWJGLException e) {
e.printStackTrace();
}
BufferedImage image = null;
try {
image = ImageIO.read(new File("BucketKitties.jpg")); //Just your average cat image
} catch (IOException e) {
e.printStackTrace();
}
int width = image.getWidth(), height = image.getHeight();
int[] pixels = image.getRGB(0, 0, width, height, new int[width*height], 0, width);
ByteBuffer data = BufferUtils.createByteBuffer(3*width*height);
//System.out.println(data);
for(int i = 0; i < pixels.length; i++){
int pixel = pixels[i];
data.put((byte) ((pixel >> 16) & 0xFF)); // Red component
data.put((byte) ((pixel >> 8) & 0xFF)); // Green component
data.put((byte) (pixel & 0xFF)); // Blue component
}
//System.out.println(data);
data.flip(); //Sets the Reading Marker to the start of the buffer
//System.out.println(data);
int texture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
//We don't need different sized images for this
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
while(!Display.isCloseRequested()){
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION); //Loads the Projection-matrix
glLoadIdentity(); //Resets the projection-matrix
//Sets a coordinate system where one pixel on the screen equals one coordinate
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW); //The "Camera Position Matrix"
glLoadIdentity();
glTranslatef(Display.getWidth()/2, Display.getHeight()/2, 0); //We want to draw with the center of the screen as (0, 0)
glBegin(GL_QUADS);
{
glColor3f(1, 1, 1);
glTexCoord2f(0, 0);
glVertex2f(-250, -250);
//glColor3f(0, 1, 0);
glTexCoord2f(1, 0);
glVertex2f(250, -250);
//glColor3f(0.5f, 0.5f, 0.5f);
glTexCoord2f(1, 1);
glVertex2f(250, 250);
//glColor3f(0, 0, 1);
glTexCoord2f(0, 1);
glVertex2f(-250, 250);
}
glEnd();
Display.sync(100);
Display.update();
}
Display.destroy();
JOptionPane.showMessageDialog(null, "Complete!");
}
}