From: Bill Kuker Date: Wed, 1 Jul 2009 02:37:38 +0000 (+0000) Subject: Added asynchronous rendering X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=71e693273f19a27190925fcfc3c7cffa375b934c;p=sw%2Fmotorsim Added asynchronous rendering --- diff --git a/src/com/billkuker/rocketry/motorsim/visual/Chart.java b/src/com/billkuker/rocketry/motorsim/visual/Chart.java index 5f5a648..db1a939 100644 --- a/src/com/billkuker/rocketry/motorsim/visual/Chart.java +++ b/src/com/billkuker/rocketry/motorsim/visual/Chart.java @@ -5,6 +5,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; import java.util.Iterator; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import javax.measure.quantity.Area; import javax.measure.quantity.Length; @@ -30,6 +32,9 @@ import com.billkuker.rocketry.motorsim.grain.CoredCylindricalGrain; public class Chart extends JPanel { private static final long serialVersionUID = 1L; + + private static ExecutorService fast = Executors.newFixedThreadPool(2) ; + private static ExecutorService slow = Executors.newFixedThreadPool(2); public class IntervalDomain implements Iterable>{ @@ -123,34 +128,55 @@ public class Chart extends JPanel { } } + + public void setDomain(final Iterable> d) { + series.clear(); + fill(d, 100); + fast.submit(new Thread(){ + public void run(){ + fill(d, 10); + } + }); + slow.submit(new Thread(){ + public void run(){ + fill(d, 1); + } + }); + } + @SuppressWarnings("unchecked") - public void setDomain(Iterable> d) { - int skip = 1; + private void fill(Iterable> d, int skip) { int sz = 0; - if ( d instanceof Collection ){ - sz = ((Collection)d).size(); - if ( sz > 200 ) - skip = sz / 200; + if (d instanceof Collection) { + sz = ((Collection) d).size(); + int sk2 = sz / 200; + if (skip < sk2) + skip = sk2; } - series.clear(); + // series.clear(); int cnt = 0; - for( Amount ax: d){ - try { - if ( cnt % skip == 0 || cnt == sz-1 ){ - Amount y = (Amount)f.invoke(source, ax); + + try { + Amount last = null; + for (Amount ax : d) { + last = ax; + if (cnt % skip == 0) { + Amount y = (Amount) f.invoke(source, ax); add(ax, y); } cnt++; - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } + Amount y = (Amount) f.invoke(source, last); + add(last, y); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } }