Reputation: 7
When ever i try to run this in eclipse as a Java project, nothing happens but if i put it all in the "public static void main(String[] args)", then it works but thats not how it's done in the video i'm learning from
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();
}
}
}
Upvotes: 0
Views: 164
Reputation: 31952
You dont need to put it ALL in the main
function, but be aware that ONLY the code put within it will be run. If your video claims otherwise, it is lying to you.
You have all your code being called from the constructor. Ideally, you would move that to the main function. If you choose to leave it as it is, you would need to create an instance of the class for it to be executed. So you can add InputDemo demo = new InputDemo();
to your main function and that might suffice..
Upvotes: 5