简体   繁体   English

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

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

I have this very weird problem that when I draw my tilemap the thread slows down by a big margin. 我有一个非常奇怪的问题,当我绘制我的tilemap时,线程的速度大大降低。

Here is tile map code: 这是图块地图代码:

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

Now here is my code for the main class which basic checks the method that draws my character and draws the tilemap before the character (Note: When I change the wait speed when the tilemap is slowing the game down, nothing happens): 现在,这是我的主类代码,该代码基本检查绘制角色并在角色之前绘制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);
    }
}

Please let me know if you need anything else! 如果您还有其他需要,请告诉我! Thanks in advance! 提前致谢!

Can you post the code for the Tile class? 您可以发布Tile类的代码吗? My best guess is that the problem may be in Tile's getImage routine. 我最好的猜测是问题可能出在Tile的getImage例程中。 For starters, I don't see anything besides you calling new for the Tiles, are you setting what image they are using somehow? 对于初学者,除了您为Tiles调用new之外,我什么都看不到,您是否设置了它们正在使用的图像? Is the getImage returning an image pre-read into memory or is it pulling the image from the hard drive every time you call getImage (this can take a long time, especially if you're trying to do it many times per frame)? 每次调用getImage时,getImage会将图像预读返回到内存中吗?还是从硬盘驱动器中提取图像(这可能会花费很长时间,特别是如果您尝试每帧执行多次)?

Also, could you post the file that you are reading in for the map? 另外,您可以将正在阅读的文件发布到地图上吗? How many characters are in it? 其中有几个字符? If it has an entire "world" map, then you're effectively re-drawing the entire world every frame. 如果它具有整个“世界”地图,那么您实际上是在每帧重新绘制整个世界。 Typically, games will only draw what can be currently seen and some border beyond what can be visually seen, this cuts down draw time dramatically for large worlds. 通常,游戏只会绘制当前可以看到的内容,并且超出视觉上可以看到的范围,这大大缩短了大世界的绘制时间。

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

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