JLabel没有重新粉刷

时间:2010-01-16 06:23:49

标签: java swing

我正在做一个显示一系列.jpg的动画师。动画师是一个正在运行的线程,因为它正在更新我用来显示文本的其他JLabel。但它不会重新绘制ImageIcon。线程类是txAnimate。怎么了?

public class main extends JFrame implements ActionListener, ItemListener, ChangeListener {

    public main() {
        try {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("FYP Video Platform");
            setResizable(true);
            setVisible(true);

            BoxLayout guiLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
            getContentPane().setLayout(guiLayout);

            displayPanel_lbl_Tx = new JPanel();
            displayPanel_lbl_Rx = new JPanel();
            displayPanel = new JPanel();
            buttonPanel_Tx = new JPanel();
            buttonPanel_Rx = new JPanel();
            selectPanel = new JPanel();
            prefixPanel = new JPanel();
            statusPanel = new JPanel();

            adddisplayPanel();
            addselectPanel();
            addprefixPanel();
            addstatusPanel();

            displayPanel.setAlignmentX(CENTER_ALIGNMENT);
            selectPanel.setAlignmentX(CENTER_ALIGNMENT);
            prefixPanel.setAlignmentX(CENTER_ALIGNMENT);
            statusPanel.setAlignmentX(CENTER_ALIGNMENT);
            this.add(displayPanel);
            this.add(selectPanel);
            this.add(prefixPanel);
            this.add(statusPanel);
            pack();

        } catch (Exception e) {
            System.out.println("Exception: " + e.toString());
        }
    }

    public void addNotify() {
        //init
        super.addNotify();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnStart_Tx) {
            if (animator == null) {
                lblDisplay_Tx.setVisible(true);
                animator = new Thread(new txAnimate());
                pauseAnimator = false;
                animator.start();
            } else if (btnStart_Tx.getText().equals("Start")) {
                pauseAnimator = false;
                animator.start();
            }

            if (btnStart_Tx.getText().equals("Resume")) {
                pauseAnimator = false;
                try {
                    synchronized (this) {
                        notify();
                    }
                } catch (Exception p) {
                    System.out.println(p);
                }
            }

            btnStart_Tx.setEnabled(false);
            btnStop_Tx.setEnabled(true);
            btnPause_Tx.setEnabled(true);
            btnFile.setEnabled(false);
            lblDisplay_Tx.setVisible(true);
        } 
    }

    private class txAnimate implements Runnable {

        public void run() {
            do {
                try {
                    lblDisplay_Tx.setVisible(true);
                    for (int i = 1; i <= (matlab.getNumFrame() - 1); i++) {
                        if (animator == null) {
                            //break the loop, when stop is pressed
                            break;
                        }

                        if ((new File(matlab.getprefix_Tx() + i + ".jpg")).exists() == false) {
                            lblStatus_Tx.setText("Buffering...");
                            Thread.sleep(1000);
                        }

                        ImageIcon images = new ImageIcon(matlab.getprefix_Rx() + i + ".jpg");
                        lblDisplay_Tx.setIcon(images);
                        lblDisplay_Tx.paintAll(lblDisplay_Tx.getGraphics());
                        Thread.sleep(matlab.getFramerate());
                        lblStatus_Tx.setText("Frame " + i + "/" + (matlab.getNumFrame() - 1));

                        synchronized (this) {
                            while (pauseAnimator) {
                                wait();
                            }
                        }
                    }
                    if (loop_Tx == false) {
                        btnStart_Tx.setText("Start");
                        btnStart_Tx.setEnabled(true);
                        btnStop_Tx.setEnabled(false);
                        btnPause_Tx.setEnabled(false);
                        btnFile.setEnabled(true);
                        lblDisplay_Tx.setVisible(false);
                        animator = null;
                    }

                } catch (InterruptedException e) {
                    System.out.println(e);
                }
                repaint();
            } while (loop_Tx);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new main();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

您需要从event dispatch thread (EDT)进行绘图,其中SwingWorker是理想的。或者,您可以使用invokeLater(),如图here所示。