每当我尝试在 Eclipse 中将其作为 Java 项目运行时,什么都没有发生,但如果我把它全部放在“public static void main(String[] args)”中,那么它可以工作,但在视频中不是这样我在学习
package Indeed;
import static org.lwjgl.opengl.GL11.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
public class InputDemo {
public static void main(String[] args) {
}
List<Box> shapes = new ArrayList<Box>(16);
public InputDemo() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Hello, LWJGL!");
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
shapes.add(new Box(15, 15));
shapes.add(new Box(100, 150));
//Initialization code OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
while(!Display.isCloseRequested())
{
//Render
glClear(GL_ACCUM_BUFFER_BIT);
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
Display.destroy();
System.exit(0);
}
for(Box box : shapes)
{
box.draw();
}
Display.update();
Display.sync(60);
}
Display.destroy();
}
private static class Box
{
public int x, y;
private float red, blue, green;
public boolean selected = false;
Box (int x, int y)
{
this.x = x;
this.y = y;
Random rand = new Random();
red = rand.nextFloat();
blue = rand.nextFloat();
green = rand.nextFloat();
}
void update(int dx, int dy)
{
x += dx;
y += dy;
}
boolean inBounds(int mouseX, int mouseY)
{
if(mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50)
{
return true;
}
else
{
return false;
}
}
void RandomColor()
{
Random rand = new Random();
red = rand.nextFloat();
blue = rand.nextFloat();
green = rand.nextFloat();
}
void draw()
{
glColor3f(red, green, blue);
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + 50, y);
glVertex2f(x+ 50, y+ 50);
glVertex2f(x, y + 50);
glEnd();
}
}
}