Hey guys, I need a bit of help trying to understand some OpenGL/LWJGL code I'm learning off of. Maybe I'm learning too quickly if I don't understand some Java concepts, but I think I'm a quick learner. :\
Maybe you guys can help me.
I split the code into parts, just to make my questions stand out.
[code]package examples;
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 {
private List<Box> shapes = new ArrayList<Box>(16);
private boolean somethingIsSelected = false;
private volatile boolean randomColorCooldown = false; //What does "Volatile" mean?
[/code]I assume try catch statements is that it TRIES to do something and if it can,it does, and if it can't it goes to the catch part which requires you to specify an exception type?[code]
public InputDemo(){
try{
Display.setDisplayMode(new DisplayMode(640,480));
Display.setTitle("Input Demo");
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_COLOR_BUFFER_BIT); //| GL_DEPTH_BUFFER_BIT); <== add that if making a 3D application
while(Keyboard.next()){
if(Keyboard.getEventKey() == Keyboard.KEY_C && Keyboard.getEventKeyState()){
shapes.add(new Box(15,15));
}
}
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
Display.destroy();
System.exit(0);
}
for(Box box : shapes){
if(Mouse.isButtonDown(0) && box.inBounds(Mouse.getX(), 480 - Mouse.getY()) && !somethingIsSelected){
somethingIsSelected = true;
box.selected = true;
System.out.println("You clicked me!");
}
if(Mouse.isButtonDown(2) && box.inBounds(Mouse.getX(), 480 - Mouse.getY()) && !somethingIsSelected){
box.randomizeColors();
randomColorCooldown = true;
[/code]//*************Can someone give me a quick rundown of Threads? If that's possible :P[code]
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(200);
} catch(InterruptedException e){
e.printStackTrace();
} finally {
randomColorCooldown = false;
}
}
}).run();
}
if(Mouse.isButtonDown(1)){
box.selected = false;
somethingIsSelected = false;
}
if(box.selected){
box.update(Mouse.getDX(),-Mouse.getDY());
}
box.draw();
}
Display.update();
Display.sync(60);
}
Display.destroy();
}
[/code]//**********You can make a class within a class? How so? :\ Or is it just something I never learned in school... I assume that since the class is labelled Private you aren't able to access it from other .java files/classes?
[code]
private static class Box{
public int x, y;
public boolean selected = false;
private float colorRed, colorBlue, colorGreen;
public Box(int x, int y){
this.x = x;
this.y = y;
Random randGen = new Random();
colorRed = randGen.nextFloat();
colorBlue = randGen.nextFloat();
colorGreen = randGen.nextFloat();
}
boolean inBounds(int mousex, int mousey){
if(mousex > x && mousex < x + 50 && mousey > y && mousey < y + 50)
return true;
else return false;
}
void update(int dx, int dy){
x += dx;
y += dy;
}
void randomizeColors(){
Random randGen = new Random();
colorRed = randGen.nextFloat();
colorBlue = randGen.nextFloat();
colorGreen = randGen.nextFloat();
}
void draw() {
glColor3f(colorRed, colorGreen, colorBlue);
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + 50, y);
glVertex2f(x + 50, y + 50);
glVertex2f(x, y + 50);
glEnd();
}
}
public static void main(String[] args){
new InputDemo();
}
}
[/code]
I hope you guys can help me :\ Sorry if it's confusing as to what I'm asking about, I'm learning! D:
Come on man, google it first, these are very widely documented and are practices present in all if not a very high percentage of programs.
[URL="http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html"]Nested Classes.[/URL]
[URL="http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html"]Try, Catch, Finally.[/URL]
[URL="http://tutorials.jenkov.com/java-concurrency/index.html"]Threading.[/URL]
[URL="http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html"]Seems you need to review access modifiers as well.[/URL]
Sorry, you need to Log In to post a reply to this thread.