Syntax Error on token 'class' @ expected [JAVA - LWJGL]
Solution 1
There should be no ()
after Box
and you should close }
at the end of your class.
Also, you should not be creating a new instance of Random
on each function call. Let it rather be a property of that class.
private static class Box {
public int x, y;
private float colorRed, colorBlue, colorGreen;
private Random randomGenerator;
public Box(int x, int y) {
this.x = x;
this.y = y;
this.randomGenerator = new Random(System.currentTimeMillis());
randomizeColors();
}
public void randomizeColors() {
colorRed = randomGenerator.nextFloat();
colorBlue = randomGenerator.nextFloat();
colorGreen = randomGenerator.nextFloat();
}
}
As for OpenGL problem, take a peek here:
http://en.wikipedia.org/wiki/Java_OpenGL
Are you sure you have all things imported and called properly? I have only worked with OpenGL in Python, but if I recall correctly, GL_QUADS were comparable to Enums in Java (or static class variables mapped to ints but with Enum-like names)
Solution 2
Your code missing end }
brace for class and class doesn;t contain ()
, Box definition should be just Box
not Box()
. Your constructor is closed and method is closed, but class is not closed.
user1448741
Updated on June 14, 2022Comments
-
user1448741 almost 2 years
I've got an error whilst coding one of my classes.
My imports are,
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.glClear; import static org.lwjgl.opengl.GL11.glLoadIdentity; import static org.lwjgl.opengl.GL11.glMatrixMode; import static org.lwjgl.opengl.GL11.glOrtho; import java.util.Random; import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; public class InputDemo{ public InputDemo(){ int height = 720; int width = 1280; try { Display.setDisplayMode(new DisplayMode(1280, 720)); Display.setTitle("Input Demonstration"); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); } //Initialization code OpenGL glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1280.0, 720.0, 0.0, 1.0, -1.0); glMatrixMode(GL_MODELVIEW); while(!Display.isCloseRequested()) { //Render (Quads are X, Y (Across, Up + Down)) glClear(GL_COLOR_BUFFER_BIT); if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { Display.destroy(); System.exit(0); } int dx = Mouse.getDX(); int dy = -Mouse.getDY(); System.out.println(dx + ", " + dy); Display.update(); Display.sync(60); } Display.destroy(); } private static class Box{ public int x, y; public boolean selected = false; private float colorRed, colorBlue, colorGreen; Box(int x, int y) { this.x = x; this.y = y; Random randomGenerator = new Random(); colorRed = randomGenerator.nextFloat(); colorBlue = randomGenerator.nextFloat(); colorGreen = randomGenerator.nextFloat(); } boolean inBounds(int mousex, int mousey) { if(mousex > x && mousex < x + 50 && mousey > y && < y + 50) return true; else return false; } void update(int dx, int dy) { x += dx; y += dy; } void randomizeColors() { Random randomGenerator = new Random(); colorRed = randomGenerator.nextFloat(); colorBlue = randomGenerator.nextFloat(); colorGreen = randomGenerator.nextFloat(); } void draw() { glBegin(GL_QUADS); glVertex2f(x, y); glVertex2f(x + 50, y); glVertex2f(x + 50, y + 50); glVertex2f(x, y + 50); glEnd(); } } /** * @param args */ public static void main(String[] args) { new InputDemo(); }
}
The error is 'Syntax error on 'class', @ expected' aswell as, 'Insert '}' to complete block'
Also, can you see an error with the draw() because I can't but glBegin isn't working as 'GL_QUADS isn't a variable, but I can't find where I've used it as a variable...'
-
user1448741 almost 12 yearsThanks a lot, that was the issue, I always do that, its just subconscious now, I'll have to remember that. Thanks so much both of you.
-
user1448741 almost 12 yearsThanks a lot, that was the issue, I always do that, its just subconscious now, I'll have to remember that. Thanks so much both of you.