java - Using a timer for my visually updating bar graph -
i have been having trouble getting timer show bar graph updating on time instead, have graph being sorted automatically though set timer update every couple of seconds. here paint , updatetextfieldthread
method.
private class display extends jpanel { private color color = color.red; public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.red); dimension d = getpreferredsize(); int clientwidth = d.width; int clientheight = d.height; int barwidth = clientwidth / array.length; int x=0, y=0; int base=410; for(int i=0;i<array.length;i++){ x+=30; y+=30; int linethickness=20; int linelength=50; g.setcolor(color.red); g.fillrect(x, base-linelength*array[i], linethickness , linelength*array[i]); g.setcolor(color.black); g.drawrect(x, base-linelength*array[i], linethickness, linelength*array[i]); } } } private class updatetextfieldthread extends swingworker<void, integer> { static final int thread_delay = 1000; protected void doinbackground() { executorservice service = executors.newsinglethreadexecutor(); try { runnable r = new runnable() { @override public void run() { insertionsort(array); display.repaint(); } }; future<?> f = service.submit(r); f.get(2, timeunit.minutes); } catch (final interruptedexception e) { // thread interrupted during sleep, wait or join } catch (final timeoutexception e) { // took long! } catch (final executionexception e) { // exception within runnable task } { service.shutdown(); } return null; } protected void process(java.util.list<integer> list) { textfield.settext("" + list.get(list.size() - 1)); } }
now revised: i'm breaking down sorting method step step , updating thread inside of still not breaking down graph.
private class buttonhandler implements actionlistener { public void actionperformed(actionevent e) { object src = e.getsource(); if (src == button1){ int[] array2=array; (int = 1; < array2.length; i++) { int thingtoinsert = array2[i]; int j = - 1; while (j >= 0 && thingtoinsert<array2[j]) { array2[j+1] = array2[j]; j--; } array2[j+1] = thingtoinsert; (new updatetextfieldthread()).execute(); } } } } private class updatetextfieldthread extends swingworker<void, integer> { static final int thread_delay = 1000; protected void doinbackground() { executorservice service = executors.newsinglethreadexecuto(); try { runnable r = new runnable() { @override public void run() { try { thread.sleep(thread_delay); display.repaint(); } catch (interruptedexception e) { e.printstacktrace(); } } }; future<?> f = service.submit(r); f.get(2, timeunit.minutes); } catch (final interruptedexception e) { // thread interrupted during sleep, wait or join } catch (final timeoutexception e) { // took long! } catch (final executionexception e) { // exception within runnable task } { service.shutdown(); } return null; }
Comments
Post a Comment