Samuel Kodytek
Samuel Kodytek

Reputation: 47

Lagg fix BufferedImage

Is there another Image Variable then buffered Image because when I launch my aplication, which reads from a text document a map, it laggs a lot

My code whith the BufferedImage(Sorry i am not English):

for(int i = 0; i < pole[0].length; i++)
    {
        for(int j = 0; j < pole.length; j++)
        {
            if(pole[j][i] == 1)
            {
                g.setColor(Color.RED);
                try {
                    //  g.fillRect(j*40, i*40, 40, 40);
                      wall = ImageIO.read(ClassLoader.getSystemResource("Images/wall.gif"));
                      g.drawImage(wall, j*40, i*40, null);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
                }
            }
        }
    }

Upvotes: 0

Views: 34

Answers (1)

Kayaman
Kayaman

Reputation: 73538

You load your image i*j times, when you need to load it only once and then use the same reference for each tile.

I.e.

Image wall = ImageIO.read("...");
for(int i=0;i < ...)
   for(int j=0;j < ...)
      g.drawImage(i*40, j*40, wall);

You shouldn't do things in loops that don't belong there, and you definitely don't want to do IO in a loop. And you absolutely definitely don't want to load the same exact picture every time in the loop, since it will not change between the loads.

Upvotes: 4

Related Questions