0

我正在编写一个程序,它将播放一首歌曲并让 JPanel 在其中显示图像。歌曲播放良好,绘制了第一张图像(我假设从最初调用paintComponent 开始),但不知何故 repaint() 似乎没有被调用。我真的可以多用一双眼睛。我有下面将显示图像的 JPanel 类的代码。非常感谢!

class pictures extends JPanel implements Runnable {
private ImageIcon images[];
private Thread imagerunner;
private int currentImage;

pictures() {
    super();
    imagerunner = new Thread(this);
    images = new ImageIcon[6];
    imagerunner = new Thread(this);
    images[0] = new ImageIcon("pic1.jpg");
    images[1] = new ImageIcon("pic2.jpg");
    images[2] = new ImageIcon("pic3.jpg");
    images[3] = new ImageIcon("pic4.jpg");
    images[4] = new ImageIcon("pic5.jpg");
    images[5] = new ImageIcon("pic6.jpg");
    currentImage = 0;
}

public void run() {
    int i = 0;
    System.out.println("starting pics");
    while( i < 100 ) {
        System.out.println("about to repaint()");
        this.repaint();
        System.out.println( "image: " + currentImage );
        waiting( 2000 );
        currentImage++;
    }
    System.out.println("done");
}

public void paintComponent( Graphics g ) {
    super.paintComponent( g );
    System.out.println("repainting");
    images[ currentImage ].paintIcon(this,g,0,0);
}

public static void waiting (int n) {
    long t0, t1;
    t0 =  System.currentTimeMillis();
    do{
        t1 = System.currentTimeMillis();
    }
    while (t1 - t0 < n);
}
}
4

3 回答 3

0

您需要执行以下操作:

1)实际创建一个实例来运行。2) 您需要定期调用 repaint() 以使您的显示器重新绘制。

希望能帮助到你。干杯!

于 2011-05-06T05:49:43.753 回答
0
  1. 你永远不会启动线程imagerunner
  2. 它被分配了两次(无缘无故)。
  3. 您不能从另一个线程修改 GUI。为此使用Swing 实用程序
于 2011-05-06T05:37:14.367 回答
0

waiting()方法似乎阻止了 EDT。最好使用 SwingTimer来安排更新。

于 2011-05-06T05:38:00.083 回答