繁体   English   中英

绘制tilemap会减慢Java中的线程速度

[英]Drawing a tilemap slows down my thread in java

我有一个非常奇怪的问题,当我绘制我的tilemap时,线程的速度大大降低。

这是图块地图代码:

public class TileMap {
    Tile[][] map;
    int x = 30, y = 0;

    public TileMap(String map) {
        String sCurrentLine;
        try {
            List<String> list = new ArrayList<String>();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/" + map)));

            while (true) {
                /** Starts reading the buffered line **/
                String line = bufferedReader.readLine();

                /** Closes the loop if there is no line to read **/
                if (line == null) {
                    bufferedReader.close();
                    break;
                }

                /** Adds a line to the list if it is not commented **/
                if (!line.startsWith("#")) {
                    list.add(line);
                }
            }

            readClass(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readClass(List<String> stringList){
        int x = 0;
        int y = 0;

        map = new Tile[stringList.get(0).length()][stringList.size()];

        for(String s : stringList){
            int i = 0;
            while (i < s.length()){
                char c = s.charAt(i);

              //  if(c == 'A'){
                    map[x][y] = new Tile();
              //  }

                i++;
                x++;
            }

            x = 0;
            y++;
        }
    }

    public void draw(Graphics g){
        for (int x = 0; x < map.length; x++) {
            for (int y = 0; y < map[0].length; y++) {
                g.drawImage(map[x][y].getImage(),  (x * 20), (y * 20), null);
            }
        }
    }
}

现在,这是我的主类代码,该代码基本检查绘制角色并在角色之前绘制tilemap的方法(注意:当我改变tilemap减慢游戏速度时的等待速度时,什么也没发生):

public class GamePane extends JPanel implements Runnable {
    public static GameStateHandler gameStateHandler = new GameStateHandler();
    private static final long serialVersionUID = 1L;
    public static int WIDTH, HEIGHT;
    public static boolean running;
    Graphics graphics;
    Thread thread;

    public GamePane(Graphics g) {
        thread = new Thread(this);
        this.graphics = g;
        init();
        thread.start();
        setFocusable(true);
        requestFocusInWindow();
        addKeyListener(new GameKeyListener(this));
    }

    public void init() {
        WIDTH = 800;
        HEIGHT = 600;
        setPreferredSize(new Dimension(WIDTH, HEIGHT));

        running = true;
    }

    public void run() {
        while (running) {
            try {
                Thread.sleep(10);
                gameStateHandler.update();
                Main.drawOffScreen();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void keyPress(int keyCode) {
        gameStateHandler.keyPressed(keyCode);
    }

    public void keyReleased(int keyCode){
        gameStateHandler.keyReleased(keyCode);
    }
}

如果您还有其他需要,请告诉我! 提前致谢!

您可以发布Tile类的代码吗? 我最好的猜测是问题可能出在Tile的getImage例程中。 对于初学者,除了您为Tiles调用new之外,我什么都看不到,您是否设置了它们正在使用的图像? 每次调用getImage时,getImage会将图像预读返回到内存中吗?还是从硬盘驱动器中提取图像(这可能会花费很长时间,特别是如果您尝试每帧执行多次)?

另外,您可以将正在阅读的文件发布到地图上吗? 其中有几个字符? 如果它具有整个“世界”地图,那么您实际上是在每帧重新绘制整个世界。 通常,游戏只会绘制当前可以看到的内容,并且超出视觉上可以看到的范围,这大大缩短了大世界的绘制时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM