Skip to content

Commit

Permalink
perf: оптимизация отрисовки
Browse files Browse the repository at this point in the history
    Использование потока вместо циклов по координатам.
  • Loading branch information
xlam committed May 30, 2019
1 parent 60d9285 commit d187d44
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/main/java/ru/cyberbiology/view/AbstractView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Arrays;
import javax.swing.JPanel;
import ru.cyberbiology.BasicBot;
import ru.cyberbiology.BasicWorld;
Expand Down Expand Up @@ -34,20 +35,22 @@ public Image paint(BasicWorld world, JPanel canvas) {

init(world, canvas);

graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, world.width * botSize, world.height * botSize);
graphics.setColor(Color.BLACK);
graphics.drawRect(0, 0, world.width * botSize + 1, world.height * botSize + 1);

for (int y = 0; y < world.height; y++) {
for (int x = 0; x < world.width; x++) {
BasicBot bot = world.getBot(x, y);
Color color = getBotColor(bot);
graphics.setColor(color);
graphics.fillRect(x * botSize, y * botSize, botSize, botSize);
if (bot != null && bot.alive == BasicBot.LV_ALIVE) {
graphics.setColor(Color.BLACK);
graphics.drawRect(x * botSize, y * botSize, botSize, botSize);
}
}
}
Arrays.stream(world.matrix)
.filter(bot -> bot != null)
.forEach(bot -> {
graphics.setColor(getBotColor(bot));
graphics.fillRect(bot.posX * botSize, bot.posY * botSize, botSize, botSize);
if (bot.alive == BasicBot.LV_ALIVE) {
graphics.setColor(Color.BLACK);
graphics.drawRect(bot.posX * botSize, bot.posY * botSize, botSize, botSize);
}
});

return buf;
}

Expand Down

0 comments on commit d187d44

Please sign in to comment.