public void run() { Refs.setSim(this); Refs.setCanvas(canvas); Refs.setEnts(ents); Refs.setMap(map); // TODO TEMP ents.add(new Creature()); // long startTime = 0; long endTime = 0; long timeElapsed = 0; int delay = 1000 / rate.getValue(); averageX = topQuarterX = 0; smooth = false; while (true) { smooth = smoothMotion.isSelected(); startTime = System.currentTimeMillis(); canvas.repaint(); map.update(); ents.update(); if (ents.hasPopulationAbove(0)) { averageX = ents.getAverageX(); topQuarterX = ents.getTopQuarterX(); } endTime = System.currentTimeMillis(); timeElapsed = endTime - startTime; delay = 1000 / rate.getValue(); Util.sleep(Util.trimInt((int) (delay - timeElapsed), 0, 99999999)); } }