After using slick for textures, game lags too much

Hi!
I made a small test game. It uses textures using slick. The textures cause ENORMOUS lag. Can someone help me?

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", this.getClass().getClassLoader().getResourceAsStream("res/" + key + ".png"));
			} catch (FileNotFoundException e) {
				
			} catch (IOException e) {
				
			}
			return null;
		}
	

}

[/spoiler]

Its probably because of the enormous loop going on in LWJGLInput. How can I reduce the lag?

Regards,
Ablaze.

You are loading the texture every time you draw.


void draw(){
-    wood = loadTexture("wood");
     wood.bind();

Move the highlighted line into the constructor.

OMG, HOW COULD I NOT THINK OF THAT? I FEEL WEIRD - HAPPY FOR MY ISSUE BEING SOLVED AND SILLY FOR BEING SO silly (lol xD)
Here is a brief description of how I feel:-

IHDSFOSFHDSFHIODSFHIODSFHDFIOSHDSFIOSIDFOHFDSIOODSFIHIDOSHADSIOHIODSFAHIOSFADHDFIOSHDSFOIHADSIOHFDIOHDSFIOSFAHIODSFHIOFDSHIOFHDOFDSHIODSFHIOF

Thanks you so much!

Regards,
Ablaze

Edit: I request mods to close this.

I had the same problem a while back, but I would advise against closing this thread… The first thing someone does when he/she has a problem in code, they often google it. If this thread were closed, then one cannot find this resource, and so would waste hours of their time if there is nothing else of the same problem!

Glad you fixed it though :slight_smile:

The mods don’t actually close threads unless they are spam, but you can’t reply to them if they are very old.