java ThiledMap slick 在当前线程中找不到 OpenGL 上下文

java ThiledMap slick No OpenGL context found in the current thread

我想使用 .tmx 文件作为 TiledMap

这是我的主class:

package com.company;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class Main {

    private static TiledMap map;
    public static void main(String[] args) {
        try {
            init();
        }
        catch (SlickException e) {
            e.printStackTrace();
        }
    }
    private static void init() throws SlickException {
        map = new TiledMap("com/company/untitled.tmx");
        map.render(100,100);
    }
}

我在intellij中编译我的程序时遇到了以下问题:

Tue Jun 20 23:37:23 IRDT 2017 ERROR:No OpenGL context found in the current thread. java.lang.RuntimeException: No OpenGL context found in the current thread. at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124) at org.lwjgl.opengl.GL11.glGetError(GL11.java:1377) at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:226) at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:171) at org.newdawn.slick.Image.(Image.java:196) at org.newdawn.slick.tiled.TileSet.(TileSet.java:113) at org.newdawn.slick.tiled.TiledMap.load(TiledMap.java:447) at org.newdawn.slick.tiled.TiledMap.(TiledMap.java:90) at org.newdawn.slick.tiled.TiledMap.(TiledMap.java:77) at com.company.Main.init(Main.java:18) at com.company.Main.main(Main.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

您不能简单地在 init 方法中开始渲染。您应该覆盖 render 方法并在那里进行渲染。那时 OpenGL 上下文应该已经被 Slick2D 初始化了。

public void render(GameContainer arg0, Graphics arg1) throws SlickException