为什么我的GUI甚至不会更新,即使正在调用repaint()?

时间:2014-09-19 11:53:24

标签: java swing sorting concurrency swingworker

我在使用摇摆工,定时器方面遇到一些困难,实际上我有点困惑。 据我所知,我必须设置一个计时器来设置必须由EDT调用的重复任务。

我试图制作一个以图形方式显示排序算法的程序(如:https://www.youtube.com/watch?v=kPRA0W1kECg

我只是不明白为什么GUI不会刷新。我非常确定正在调用重绘方法,因为我将一个sysout显示给我有序的值,它似乎有效,但GUI只是...没有改变。

这是我的代码:

public class MainWindow {
    private JFrame frame;
    JPanel panel;
    public final static int JFRAME_WIDTH = 800;
    public final static int JFRAME_HEIGHT = 600;
    public final static int NELEM = 40;

    ArrayList<Double> numbers;
    ArrayList<myRectangle> drawables = new ArrayList<myRectangle>();
    Lock lock = new ReentrantLock();
    Condition waitme = lock.newCondition();


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainWindow window = new MainWindow();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MainWindow() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, JFRAME_WIDTH + 20, JFRAME_HEIGHT + 40);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new myPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        Timer timer = new Timer(500, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                lock.lock();
                try{
                    //Updating the gui
                    panel.repaint();
                    panel.revalidate();
                    //Giving the OK to the sorting alghoritm to proceed.
                    waitme.signal();
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    lock.unlock();
                }
            }
        });
        timer.start();

        SwingWorker<Integer, String> sw = new SwingWorker<Integer, String>(){

            @Override
            protected Integer doInBackground() throws Exception {
                mapAndCreate();
                bubbleSort();
                return null;
            }

        };
        sw.execute();
    }

    private void bubbleSort() throws InterruptedException{      
        for(int i=0; i < NELEM; i++){
            for(int j=1; j < (NELEM-i); j++){
                if(drawables.get(j-1).wid > drawables.get(j).wid){
                    //swap the elements!
                    myRectangle temp = drawables.get(j-1);
                    drawables.set(j-1, drawables.get(j));
                    drawables.set(j, temp);
                    lock.lock();
                    try{
                        //Wait for the GUI to update.
                        waitme.await();
                    }catch(Exception e){
                        e.printStackTrace();
                    }finally{
                        lock.unlock();
                    }
                }
            }
        }

    }

    /***
     * Function that maps values from 0 to 1 into the rectangle width.
     */
    private void mapAndCreate() {
        double max = 0;
         numbers = new ArrayList<Double>(NELEM);
        //Finding maximum.
        for(int i = 0; i < NELEM; i++){
            Double currElem = Math.random();
            if(currElem > max) max = currElem;
            numbers.add(currElem);
        }
        //Mapping process
        int offset = 0;
        for(int j = 0; j < NELEM; j++){
            Integer mapped = (int) (( JFRAME_WIDTH * numbers.get(j) ) / max);
            myRectangle rect = new myRectangle(offset , mapped);
            drawables.add(rect);
            offset += JFRAME_HEIGHT / NELEM;
        }
    }

    private class myRectangle{

        int myy , wid , colorR,colorG,colorB;

        public myRectangle(int y , int wid){
            this.myy = y;
            this.wid = wid;
            Random r = new Random();
            colorR = r.nextInt(255);
            colorG = r.nextInt(255);
            colorB = r.nextInt(255);

        }
    }

    private class myPanel extends JPanel{

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for(myRectangle rectan : drawables){
                Graphics2D graphics2D = (Graphics2D)g;
                System.out.println(rectan.wid);
                Rectangle2D.Double rect = new Rectangle2D.Double(0,rectan.myy,rectan.wid,JFRAME_HEIGHT / NELEM);
                graphics2D.setColor(new Color(rectan.colorR,rectan.colorG,rectan.colorB));
                graphics2D.fill(rect);
            }
            System.out.println("====================================================================================================");
        }
    }


}

1 个答案:

答案 0 :(得分:3)

大多数操作系统(或者更确切地说是他们使用的UI框架)不支持并发访问。简单地说,你不能同时渲染两个文本字符串。

这就是为什么Swing在UI线程中运行所有渲染操作的原因。在UI线程之外调用呈现函数(如paint())可能会导致各种问题。因此,当你这样做时,Swing会记住“我应该重新绘制”并返回(而不是做任何实际的工作)。这样,Swing可以保护您,但是大多数人都希望通过有用的消息获得错误。

计时器总是意味着在计时器用完时有一个执行的线程。这不是Swing的UI线程。因此,任何paing操作必须包含EventQueue.invokeLater()或类似的。

另一个常见错误是占用UI线程(因此不会进行渲染,因为那里进行了复杂的计算)。这就是SwingWorker的用途。同样,在SwingWorker的大多数方法中,禁止调用某些东西的方法( - &gt;使用invokeLater())。

所以我的猜测是UI线程等待锁定并且锁定不会提前或经常解锁。 See this demo如何在Swing中做一个简单的动画。

public class TimerBasedAnimation extends JPanel implements ActionListener {
  public void paint(Graphics g) {
      // setup
      // do some first-run init stuff
      // calculate the next frame
      // render frame
  }

  public void actionPerformed(ActionEvent e) {
    repaint();
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("TimerBasedAnimation");
    frame.add(new TimerBasedAnimation());
    ...
  }
}

正如您在代码中看到的那样,没有锁定。相反,您只需将“{now 1”}事件从actionPerformed发送到Swing。一段时间后,Swing会拨打paint()。当发生这种情况时,没有任何说法(也没有办法确定或强制使用Swing)。

如此好的动画代码将占用当前时间,计算当时的动画状态,然后渲染它。所以它不会盲目地在M秒内逐步完成N个阶段。相反,它会针对每一帧进行调整,以创建一种幻觉,即动画实际上并非如此。

相关:

相关问题