-1
\$\begingroup\$

hi I have a game where it reads a txt file and loads a map 0 represents air and 2 represents dirt I have a class for each tile as well as a tilemap I will show u my code. the problem is it lags like all hell. is there something I am doing wrong or is there something I need to add like rendering a certain amount of tiles at a time.(I don't know how to do that).it lags really bad. here is my code:

public class tilemap {

private int x;
private int y;

private int tilesize;
private static int[][] map;
private int mapwidth;
private int mapheight;
public static Image img;
public static int camx,camy;
public static BufferedImage stone;

public ArrayList<air> air = new ArrayList<air>();
public ArrayList<dirt> dirt = new ArrayList<dirt>();

public String sl;

public tilemap(String s,int tilesize){
    this.tilesize = tilesize;

    try{

        BufferedReader br = new BufferedReader(new FileReader(s));

        mapwidth = Integer.parseInt(br.readLine());
        mapheight = Integer.parseInt(br.readLine());

        map = new int[mapheight][mapwidth];

        String del = " ";
        for(int row = 0; row < mapheight; row++){
            String line = br.readLine();
            String[] tokens = line.split(del);
            for(int col = 0; col < mapwidth; col++){
                map[row][col] = Integer.parseInt(tokens[col]);
            }
        }

    }catch(Exception e){}

    try{
    }catch(Exception e){

    }
}


public void update(){
    //checkcollision();
      for (int i = 0; i < air.toArray().length; i++) {
            air.get(i).tick();

        }
      for (int i = 0; i < dirt.toArray().length; i++) {
            dirt.get(i).tick();

        }

}






public void draw(Graphics g){
    for(int row = 0; row < mapheight; row++){
        for(int col = 0; col < mapwidth; col++){

             int rc = map[row][col];

             int mr = tilesize*row;
             int mc = tilesize*col;

              if(rc == 0){
                  air.add(new air(mc,mr));
              }
              if(rc == 2){
                  dirt.add(new dirt(mc,mr));
              }

              for (int i = 0; i < air.toArray().length; i++) {
                air.get(i).render(g);

              }


              for (int i = 0; i < dirt.toArray().length; i++) {
                dirt.get(i).render(g);

              }


        }
    }

}

}

public class air {

public int y;
public int x;
public Image img;

public Rectangle r = new Rectangle(x,y,40,40);

public air(int x,int y){
    this.x = x;
    this.y = y;
}
public void tick(){

}

public void render(Graphics g){
     ImageIcon i2 = new ImageIcon("res/water.png");
        img = i2.getImage();
        g.drawImage(img,x - tilemap.camx,y-tilemap.camy,null);   

}

}

public class dirt {

public int y;
public int x;
public Image img;

public Rectangle r = new Rectangle(x,y,40,40);

public dirt(int x,int y){
    this.x = x;
    this.y = y;
}
public void tick(){

}

public void render(Graphics g){
     ImageIcon i2 = new ImageIcon("res/dirt.png");
        img = i2.getImage();
        g.drawImage(img,x - tilemap.camx,y-tilemap.camy,null);   

}

}

\$\endgroup\$
2
\$\begingroup\$

You are loading a graphic for every tile in every frame. Load it once at application startup that sould improve the speed dramatically.

\$\endgroup\$
  • \$\begingroup\$ how do u suggest I do this \$\endgroup\$ – user2957632 Feb 1 '14 at 17:25
  • \$\begingroup\$ You could make the image a static field of the class \$\endgroup\$ – ooxi Feb 4 '14 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.