improved repaint efficiency and fixed concurrent modification bug

This commit is contained in:
Fredrik Osterlind 2012-06-05 13:16:09 +02:00
parent a8a3c7b01a
commit afcc7559a5

View File

@ -69,14 +69,16 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
private Visualizer visualizer = null; private Visualizer visualizer = null;
private AbstractRadioMedium radioMedium = null; private AbstractRadioMedium radioMedium = null;
private ArrayList<RadioConnectionArrow> history = new ArrayList<RadioConnectionArrow>(); private ArrayList<RadioConnectionArrow> historyList = new ArrayList<RadioConnectionArrow>();
private RadioConnectionArrow[] history = null;
private Observer radioMediumObserver = new Observer() { private Observer radioMediumObserver = new Observer() {
public void update(Observable obs, Object obj) { public void update(Observable obs, Object obj) {
RadioConnection last = radioMedium.getLastConnection(); RadioConnection last = radioMedium.getLastConnection();
if (last != null && history.size() < MAX_HISTORY_SIZE) { if (last != null && historyList.size() < MAX_HISTORY_SIZE) {
history.add(new RadioConnectionArrow(last)); historyList.add(new RadioConnectionArrow(last));
visualizer.repaint(); history = historyList.toArray(new RadioConnectionArrow[0]);
visualizer.repaint(500);
} }
} }
}; };
@ -86,11 +88,11 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
return; return;
} }
if (history.size() > 0) { if (historyList.size() > 0) {
boolean hasOld = false; boolean hasOld = false;
/* Increase age */ /* Increase age */
for (RadioConnectionArrow connArrow : history) { for (RadioConnectionArrow connArrow : historyList) {
connArrow.increaseAge(); connArrow.increaseAge();
if(connArrow.getAge() >= connArrow.getMaxAge()) { if(connArrow.getAge() >= connArrow.getMaxAge()) {
hasOld = true; hasOld = true;
@ -99,15 +101,16 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
/* Remove too old arrows */ /* Remove too old arrows */
if (hasOld) { if (hasOld) {
RadioConnectionArrow[] historyArr = history.toArray(new RadioConnectionArrow[0]); RadioConnectionArrow[] historyArr = historyList.toArray(new RadioConnectionArrow[0]);
for (RadioConnectionArrow connArrow : historyArr) { for (RadioConnectionArrow connArrow : historyArr) {
if(connArrow.getAge() >= connArrow.getMaxAge()) { if(connArrow.getAge() >= connArrow.getMaxAge()) {
history.remove(connArrow); historyList.remove(connArrow);
} }
} }
historyArr = historyList.toArray(new RadioConnectionArrow[0]);
} }
visualizer.repaint(); visualizer.repaint(500);
} }
/* Reschedule myself */ /* Reschedule myself */
@ -120,10 +123,12 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
this.simulation = simulation; this.simulation = simulation;
this.visualizer = vis; this.visualizer = vis;
this.active = true; this.active = true;
history.clear();
simulation.invokeSimulationThread(new Runnable() { simulation.invokeSimulationThread(new Runnable() {
public void run() { public void run() {
historyList.clear();
history = null;
/* Start observing radio medium for transmissions */ /* Start observing radio medium for transmissions */
radioMedium.addRadioMediumObserver(radioMediumObserver); radioMedium.addRadioMediumObserver(radioMediumObserver);
@ -179,8 +184,11 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
} }
public void paintBeforeMotes(Graphics g) { public void paintBeforeMotes(Graphics g) {
RadioConnectionArrow[] historyArr = history.toArray(new RadioConnectionArrow[0]); RadioConnectionArrow[] historyCopy = history;
for (RadioConnectionArrow connArrow : historyArr) { if (historyCopy == null) {
return;
}
for (RadioConnectionArrow connArrow : historyCopy) {
float colorHistoryIndex = (float)connArrow.getAge() / (float)connArrow.getMaxAge(); float colorHistoryIndex = (float)connArrow.getAge() / (float)connArrow.getMaxAge();
g.setColor(new Color(colorHistoryIndex, colorHistoryIndex, 1.0f)); g.setColor(new Color(colorHistoryIndex, colorHistoryIndex, 1.0f));
Radio source = connArrow.getConnection().getSource(); Radio source = connArrow.getConnection().getSource();
@ -203,13 +211,15 @@ public class TrafficVisualizerSkin implements VisualizerSkin {
private static class RadioConnectionArrow { private static class RadioConnectionArrow {
private RadioConnection conn; private RadioConnection conn;
private int age; private int age;
private final int MAX_AGE = 10; private static final int MAX_AGE = 10;
RadioConnectionArrow(RadioConnection conn) { RadioConnectionArrow(RadioConnection conn) {
this.conn = conn; this.conn = conn;
this.age = 0; this.age = 0;
} }
public void increaseAge() { public void increaseAge() {
age++; if (age < MAX_AGE) {
age++;
}
} }
public int getAge() { public int getAge() {
return age; return age;