Note that there are some explanatory texts on larger screens.

plurals
  1. POSwing animation running extremely slow
    text
    copied!<p>I have a problem with my current animation that I'm running using Java Swing. It is a discrete event simulation and the text based simulation is working fine, I'm just having problems connecting the simulating to GUI output.</p> <p>For this example I will have 10 cars to be simulated. The cars are represented by <code>JPanels</code> which I will elaborate on in a few moments.</p> <p>So consider, the event process_car_arrival. Every time this event is scheduled for execution, I'm adding a <code>Car</code> object to an <code>ArrayList</code> called <code>cars</code> in my <code>Model</code> class. The <code>Car</code> class has the following relevant attributes:</p> <pre><code>Point currentPos; // The current position, initialized in another method when knowing route. double speed; // giving the speed any value still causes the same problem but I have 5 atm. RouteType route; // for this example I only consider one simple route </code></pre> <p>In addition it has the following method <code>move()</code> :</p> <pre><code>switch (this.route) { case EAST: this.currentPos.x -= speed; return this.currentPos; . . . //only above is relevant in this example </code></pre> <p>This is all well. so in theory the car traverses along a straight road from east to west as I just invoke the <code>move()</code> method for each car I want to move.</p> <p>Returning to the process_car_arrival event. After adding a Car object it invokes a method <code>addCarToEast()</code> in the <code>View</code> class. This adds a JPanel at the start of the road going from east to west.</p> <p>Going to the <code>View</code> class now I have a ** separate** thread which does the following ( the run() method) :</p> <pre><code>@Override public void run() { while (true) { try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } if (!cars.isEmpty()) { cars.get(i).setLocation( new Point(getModel.getCars().get(i).move())); if (i == cars.size() - 1) { i = 0; } else { i++; } } } } </code></pre> <p>The above does move the car from east to west smoothly at first. But after there is 3-4 cars moving it just ends up being EXTREMELY slow and when I have 10 cars moving it just ends up moving very little.</p> <p>Just to clear up, at the moment in the <code>Model</code> class there's an <code>ArrayList</code> of <code>Car</code> objects, and in the <code>View</code> class there is also an <code>ArrayList</code> of <code>JPanel</code> objects representing the cars. I'm trying to match the <code>Car</code> objects to the <code>JPanels</code>, but I'm obviously doing a cra**y job. </p> <p>I suspect that I'm doing something insanely inefficient but I don't know what. I thought initially maybe it's accessing the <code>ArrayList</code> so much which I guess would make it really slow.</p> <p>Any pointers to what I can change to make it run smoothly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload