SE450: Project: Notes [13/13] Previous pageContents

A ConcurrentModificationException happens when you modify a collection
while iterating through it.  For example:

for (Agent a : Model.agents) {
  GrimReaper.delete(c)
}

GrimReaper {
  delete(Agent a) { Model.agents.remove(a);
}

This will cause a problem, since the grim reaper modifies the
collection that is being iterated through.  You solve the problem by
iterating through a copy:

for (Agent a : Model.agents.copy()) {
  GrimReaper.delete(c)
}

Now the copy is being iterated through, not the original, so no problems.  

-----------------------------------------------------------------------

If I follow the lecture correctly, it seems we only need to modify
SwingAnimationPainterBuilder.java, is that correct?  I know we need to
use the builder to add model components to the model, but is there
other visualization code that we need to touch?

-----------------------------------------------------------------------

I had to edit a lot of things in the animation classes to get  
everything playing nice with the graphics.  The given graphics library  
does not take care of the following:

- Changing of colors (traffic lights)
- Proportionate lengths of roads and intersecitons (i.e., a road may  
look the same size but it's actually longer than the next)
- Drawing moving dots (cars) on the intersections

So I assumed that it was up to us to fix these issues.  I went ahead  
and added the above features as necessary.  The colors of the lights  
can be done through the same sate pattern that controls the traffic.

The lengths of roads can be taken care of by using an equation that  
"normalizes" position of cars so that no matter what the visual is  
drawn up to be the position is normalized to a 0 to 1 double where 0  
is the beginning of the segment or intersection (or whatever the next  
extension you decide to draw) and 1 is the end.

I also had to add builder functions to let the animation know about  
intersections since the cars were disappearing at each intersection.  
It took me a while to figure out that they were still being processed  
but not being drawn.  When the visual of the car goes away it's really  
confusing because it looks like it teleported even though it was still  
moving all along.

Another tip: increase the size of the traffic light dots so they are  
bigger than cars.  This way if there is a lot of gridlock action, you  
can still see the state of each light.  I've got a screenshot here.

-----------------------------------------------------------------------
Can you explain this a bit further?

"The lengths of roads can be taken care of by using an equation that
"normalizes" position of cars so that no matter what the visual is
drawn up to be the position is normalized to a 0 to 1 double where 0
is the beginning of the segment or intersection (or whatever the
next
extension you decide to draw) and 1 is the end."

I think maybe this is my problem with my cars being drawn stopped in
the middle of the next road when they are supposed to be stopped at
the intersection when the light is red.

Are you saying to do something like this...

double roadLength = e.x.getLength();
double normalizedPosition = pos/roadLength;
XGraphics.fillOval(g, e.t, normalizedPosition, 0, MP.dotLength,
VP.elementWidth);

When doing it that way my cars no longer go past a blocked
intersection, but they stay at the start of the line, instead of going
up to the intersection.  Hmmmm.

-----------------------------------------------------------------------

You are REAL close.  Now all you have to do is take care of the offset  
that the dot imposes.  The graphics engine draws the center of the dot  
at "position".  I think this will get you moving in the right  
direction.  Are you using the "frontPosition"?

Also, I just put the formula in the argument for the fillOval  
function.  Then you don't really need to create all the extra  
doubles.  To do this I also added a method to Car called  
GetRoadObjectLength() which returns a double that is the length of the  
road object that the car is currently on.

Oh! and I forgot one more thing.  You now have a position from 0 to  
1.  That wouldn't show much movement for you now would it?  That must  
be why your cars are staying at the front of the road.  Actually if  
you were to look REAL close you would see the car move 1 meter on a  
200 meter road!  You forgot to multiply the "normalized" distance with  
the graphical length of the road, MP.lineLength.

What's real cool about this equation? Go ahead and change the  
MP.lineLength value.  Make it 300 if you like and watch as the  
equation takes care of all your problems!

Previous pageContents