Java 使用计时器直观地更新条形图

Java 使用计时器直观地更新条形图,java,swing,timer,Java,Swing,Timer,我一直很难让我的计时器显示我的条形图随着时间的推移而自动更新,我所拥有的是我的图形被自动排序,即使我将计时器设置为每隔几秒钟更新一次。 这是我的绘画和updatetextfieldthread方法 private class Display extends JPanel { private Color color = Color.RED; public void paintComponent(Graphics g) { super.paintCompon

我一直很难让我的计时器显示我的条形图随着时间的推移而自动更新,我所拥有的是我的图形被自动排序,即使我将计时器设置为每隔几秒钟更新一次。 这是我的绘画和
updatetextfieldthread
方法

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) {
                    // The thread was interrupted during sleep, wait or join
                }
                catch (final TimeoutException e) {
                    // Took too long!
                }
                catch (final ExecutionException e) {
                    // An exception from within the Runnable task
                }
                finally {
                    service.shutdown();
                }
            return null;



        }


        protected void process(java.util.List<Integer> list)
        {
            textfield.setText("" + list.get(list.size() - 1));
        }
    }
私有类显示扩展了JPanel{
私有颜色=Color.RED;
公共组件(图形g){
超级组件(g);
g、 setColor(Color.RED);
维度d=getPreferredSize();
int clientWidth=d.width;
int clientHeight=d.高度;
int barWidth=clientWidth/array.length;
int x=0,y=0;
int base=410;

对于(iTi=0;IUY<代码>插入排序< /COD>方法未被分级/步进,一旦方法返回,排序已完成,因此,没有更新……记住…,动画是随时间变化的幻象。因此,每次调用<代码>运行< /COD>方法时,您需要移动到下一个状态。很好的方法…我会考虑一个摆动<代码>定时器< /代码>,只要你可以重新编写你的<代码>插入排序< /COD>方法,而不是一个完整的排序。看到你正在返回<代码> null <代码>,但是返回类型无效吗?好吧,我试着使用定时器很多,但是不能真正使它工作,但是我几乎得到了什么。我一直在寻找使用线程的方法。我想我只漏掉了一行,现在我觉得它就在我的嘴边。我一直在使用线程延迟让我的图表暂停,但它仍然漏掉了在步骤中显示它的步骤,我现在已经将插入排序代码移动到actionevent方法,我正在meth中从内部调用线程但它仍然拒绝采取正确的行动…我将继续与它的混乱,但任何帮助是感激的(我张贴了上面的一点)。
private class ButtonHandler implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {
         Object src = e.getSource();
         if (src == button1){

           int[] array2=array;
           for (int i = 1; i < array2.length; i++) {
                int thingToInsert = array2[i];

                int j = i - 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) {
                    // The thread was interrupted during sleep, wait or join
                }
                catch (final TimeoutException e) {
                    // Took too long!
                }
                catch (final ExecutionException e) {
                    // An exception from within the Runnable task
                }
                finally {
                    service.shutdown();
                }
            return null;




        }