我正在编写一个程序,播放一首歌并在其中显示一个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);
}
}
答案 0 :(得分:0)
imagerunner
。答案 1 :(得分:0)
waiting()
方法似乎阻止了EDT。最好使用Swing Timer
来安排更新。
答案 2 :(得分:0)
您需要执行以下操作:
1)实际创建一个要运行的实例。 2)您需要定期调用repaint()以使您的显示重新绘制。
希望它有所帮助。干杯!