Just a little repair of above code. Warning this code has been done in combination with my very first window so… SAVE all of your work first.
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import org.lwjgl.;
import org.lwjgl.opengl.;
import org.lwjgl.input.*;
/** Main class
*/
public final class leson06l {
//static org.lwjgl.Window Window;
static GL Window=new GL(" ",24,8,16,0);
// GL context creation…
static {
try {
Window.create(); // Create The Opengl Context
} catch (Exception e) {
System.exit(1);
}
}
/** Global variable declaration */
private static boolean finished;
private static float xrot; // X Rotation
private static float yrot; // Y Rotation
private static float zrot; // Z Rotation
private static int texture; // Storage For One Texture
/**
- Constructor (unused here)
*/
private leson06l() {
}
/**
- Main method
*/
public static void main(String args[]) {
try {
init(); // Init Opengl
while (!finished) {
Keyboard.poll(); // Poll The Keyboard
mainLoop(); // Launch The Main Loop
render(); // Render To Screen
Window.tick();
Window.paint(); // Swap Opengl Buffers
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
cleanup();
}
}
/**
- Init opengl
*/
private final static void init() throws Exception {
texture = loadTexture("data/nehe.png");
Keyboard.create(); // Create The Keyboard
GL.glEnable(GL.GL_TEXTURE_2D); // Enable Texture Mapping
GL.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
GL.glClearDepth(1.0); // Depth Buffer Setup
GL.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
GL.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do
GL.glMatrixMode(GL.GL_PROJECTION); // Select The Projection Matrix
GL.glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(
45.0f,
(float) Display.getWidth() / (float) Display.getHeight(),
0.1f,
100.0f);
GL.glMatrixMode(GL.GL_MODELVIEW); // Select The Modelview Matrix
// Really Nice Perspective Calculations
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
//sync to monitor
//GLCaps.determineAvailableExtensions();
//if(GLCaps.WGL_EXT_swap_control) {
//GL.wglSwapIntervalEXT(1);
// }
}
/**
- Main loop
*/
private final static void mainLoop() {
processKeyboard(); // Get Keyboard Events
processWindow(); // Get Window Events
}
/**
- Rendering method
*/
private final static void render() {
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
GL.glLoadIdentity(); // Reset The Current Modelview Matrix
GL.glTranslatef(0.0f, 0.0f, -5.0f);
GL.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
GL.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
GL.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
GL.glBindTexture(GL.GL_TEXTURE_2D, texture);
GL.glBegin(GL.GL_QUADS); {
// Front Face
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3f(-1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3f(1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3f(1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3f(-1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3f(-1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3f(1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3f(1.0f, -1.0f, -1.0f);
// Top Face
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3f(-1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3f(-1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3f(1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3f(1.0f, 1.0f, -1.0f);
// Bottom Face
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3f(-1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3f(1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3f(1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3f(1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3f(1.0f, 1.0f, -1.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3f(1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3f(1.0f, -1.0f, 1.0f);
// Left Face
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3f(-1.0f, -1.0f, -1.0f);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3f(-1.0f, -1.0f, 1.0f);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3f(-1.0f, 1.0f, 1.0f);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3f(-1.0f, 1.0f, -1.0f);
}
GL.glEnd();
xrot += 0.3f;
yrot += 0.2f;
zrot += 0.4f;
}
/**
- Process keyboard events
*/
private final static void processKeyboard() {
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
finished = true;
}
/**
- Process window events
*/
private final static void processWindow() {
if (Window.isCloseRequested()) {
finished = true;
}
}
/**
- Cleanup
*/
private final static void cleanup() {
Keyboard.destroy(); // Destroy The Keyboard
Window.destroy(); // Destroy The Opengl Context
}
/**
- Load a texture in OpenGL memory
*/
private final static int loadTexture(String path) {
Image image = (new javax.swing.ImageIcon(path)).getImage();
// Exctract The Image
BufferedImage tex =
new BufferedImage(
image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = (Graphics2D) tex.getGraphics();
g.drawImage(image, null, null);
g.dispose();
// Flip Image
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op =
new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
tex = op.filter(tex, null);
// Put Image In Memory
ByteBuffer scratch =
ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
byte data[] =
(byte[]) tex.getRaster().getDataElements(
0,
0,
tex.getWidth(),
tex.getHeight(),
null);
scratch.clear();
scratch.put(data);
scratch.rewind();
// Create A IntBuffer For Image Address In Memory
IntBuffer buf =
ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer( );
GL.glGenTextures(16,buf); // Create Texture In OpenGL
GL.glBindTexture(GL.GL_TEXTURE_2D, buf.get(0));
// Typical Texture Generation Using Data From The Image
// Linear Filtering
GL.glTexParameteri(
GL.GL_TEXTURE_2D,
GL.GL_TEXTURE_MIN_FILTER,
GL.GL_LINEAR);
// Linear Filtering
GL.glTexParameteri(
GL.GL_TEXTURE_2D,
GL.GL_TEXTURE_MAG_FILTER,
GL.GL_LINEAR);
// Generate The Texture
GL.glTexImage2D(
GL.GL_TEXTURE_2D,
0,
GL.GL_RGB,
tex.getWidth(),
tex.getHeight(),
0,
GL.GL_RGB,
GL.GL_UNSIGNED_BYTE,
scratch);
return buf.get(0); // Return Image Address In Memory
}
}
Now it should compile and run