0

I have a collections of agents (objects), when the simulator begins each agent performs a act() method which updates their location in the environment (2D Array). My paintComponent(Graphics g) is overridden in a different class (which basically iterates through the 2D array and draws a rectangle colour corresponding to the type of agent).

But the actual collection is in the simulator class which run independently by the user clicking a button from the GUI. If I put the repaint() method at the end of the paintComponent(Graphics g). It only updates the JPanel canvass twice, once when the application start-up and second when I click the run button that starts simulation.

How do I have the JPanel canvass update automatically with the new image when all the agents perform the act method. Or perhaps when a change happens in a collection to a agent draw the new location on the canvass and remove the old one ?

  • 1
    You need some way for your model to tell the UI that something has changed. You could employ a observer pattern using something a `ChangeListener` for example – MadProgrammer Jan 28 '14 at 00:35
  • I was thinking about that but Ive never used the observer pattern any example of using observer pattern within a simulation would be appreciated – user3112844 Jan 28 '14 at 00:38
  • This [example](http://stackoverflow.com/a/3072979/230513) illustrates the _observer pattern_. – trashgod Jan 28 '14 at 02:30

0 Answers0