Hi!
I made a small puzzle with textures. I used lwjgl.jar, lwjgl_util.jar and slick-util.jar. It works fine in eclipse but not when exported.
Main.java
[spoiler]
package example;
public class Main {
public static void main(String[] args) {
new LWJGLInput();
}
}
[/spoiler]
LWJGLInput.java
[spoiler]
package example;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class LWJGLInput {
List<Box> boxes = new ArrayList<Box>(16);
boolean select = false;
public Texture loadTexture(String key){
try {
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public LWJGLInput(){
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Puzzle");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
//65
boxes.add(new Box(15, 15));
boxes.add(new Box(200, 150));
boxes.add(new Box(15, 115));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
while (!Display.isCloseRequested()){
glClear(GL_COLOR_BUFFER_BIT);
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
Display.destroy();
System.exit(0);
}
for(Box box : boxes){
if(Mouse.isButtonDown(0) && box.isInBounds(Mouse.getX(), 480 - Mouse.getY()) && !select && (boxes.indexOf(box)==1)){
box.selected = true;
select = true;
}
if(Mouse.isButtonDown(1)){
box.selected = false;
select = false;
}
if (box.selected){
box.update(Mouse.getDX(), -Mouse.getDY());
}
if(boxes.indexOf(box) == 1 && (box.x == 15) && (box.y == 65)){
if(!box.selected){
while(true){
System.out.println("You win the game!");
System.out.println("Thanks for playing! :)");
System.exit(0);
}
}
}
box.draw();
}
Display.update();
Display.sync(60);
}
Display.destroy();
}
}
[/spoiler]
Box.java
[spoiler]
package example;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glVertex2f;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class Box {
Texture wood;
int x,y;
float cRed, cBlue, cGreen;
public boolean selected = false;
Box(int x, int y){
this.x = x;
this.y = y;
Random ran = new Random();
cRed = ran.nextFloat();
cBlue = ran.nextFloat();
cGreen = ran.nextFloat();
}
void randomize(){
Random ran = new Random();
cRed = ran.nextFloat();
cBlue = ran.nextFloat();
cGreen = ran.nextFloat();
}
void draw(){
wood = loadTexture("wood");
wood.bind();
int a = x;
int b = y;
int c = x+50;
int d = y + 50;
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(a, b);
glTexCoord2f(1,0);
glVertex2f(c, b);
glTexCoord2f(1,1);
glVertex2f(c, d);
glTexCoord2f(0,1);
glVertex2f(a, d);
glEnd();
}
void update(int dx, int dy){
x += dx;
y += dy;
}
boolean isInBounds(int mouseX, int mouseY) {
return mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50;
}
public Texture loadTexture(String key){
try {
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return null;
}
}
[/spoiler]
I used jarsplice to compile it all. In case you want to inspect the jar file through winrar, here it is:-
https://www.mediafire.com/?oba961abs7sysw3
So here is the problem. I try to run it (double click it), it comes for a second and disappears. This doesn’t happen in eclipse.
I tried running it through the command prompt and I got an exception - NullPointer.
[spoiler]
Exception in thread "main" java.lang.NullPointerException
at example.Box.draw(Box.java:44)
at example.LWJGLInput.<init>(LWJGLInput.java:104)
at example.Main.main(Main.java:5)
[/spoiler]
If it isn’t null in eclipse, why does the variable become null when exported?
Please help me.
Regards,
Ablaze.