令牌'class'@ expected上的语法错误[JAVA - LWJGL]

时间:2012-07-17 14:12:20

标签: java opengl lwjgl

我在编写其中一个课程时遇到错误。

我的进口是,

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();
}

}

错误是'class'上的语法错误,@ expected' 以及, '插入'}'完成阻止'

另外,你能看到draw()的错误,因为我不能,但glBegin不能正常工作'GL_QUADS不是变量,但我找不到我把它用作变量的地方...'

2 个答案:

答案 0 :(得分:4)

()之后应该没有Box,你应该在课程结束时关闭}

此外,您不应该在每个函数调用上创建Random的新实例。让它成为那个阶级的财产。

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();
    }
}

至于OpenGL问题,请看一下:
http://en.wikipedia.org/wiki/Java_OpenGL

您确定所有东西都已导入并正确调用吗? 我只使用过Python中的OpenGL,但是如果我没记错的话,GL_QUADS可以与Java中的Enums(或映射到整数的静态类变量但具有类似Enum的名称)相媲美

答案 1 :(得分:3)

您的代码缺少对类和类的结束}大括号;不包含(),Box定义应该只是Box而不是Box()。您的构造函数已关闭,方法已关闭,但类未关闭。