简体   繁体   中英

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.

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):

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? My best guess is that the problem may be in Tile's getImage routine. For starters, I don't see anything besides you calling new for the Tiles, are you setting what image they are using somehow? 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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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