Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error on token 'class' @ expected [JAVA - LWJGL]

Tags:

java

opengl

lwjgl

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...'

like image 280
user1448741 Avatar asked Jul 17 '12 14:07

user1448741


2 Answers

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)

like image 66
ioreskovic Avatar answered Oct 31 '22 17:10

ioreskovic


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.

like image 23
kosa Avatar answered Oct 31 '22 18:10

kosa