JLabel is not repainting
I am doing an animator showing a series of .jpg. The animator is a thread which is running as it is updating the other JLabel which I use to display text. But it just will not repaint the ImageIcon. The thread class is txAnimate. What's wrong?
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);
th开发者_如何转开发is.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();
}
});
}
}
You need to do your drawing from the event dispatch thread (EDT), for which a SwingWorker is ideal. Alternatively, you can use invokeLater()
, as shown here.
精彩评论