我正在編寫一個程序,它將播放一首歌曲並在其中顯示一個JPanel圖像。歌曲播放良好,第一個圖像被繪製(我從最初調用paintComponent時假定),但不知何故repaint()似乎沒有被調用。我真的可以使用額外的眼睛。我有下面的代碼來顯示圖像的JPanel類。非常感謝!在JPanel Runnable中重繪問題
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);
}
}
你如何稱此類的代碼?你在哪裏打電話給你的線程啓動()? – 2011-05-06 05:36:55
爲了更快提供更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。順便說一句 - 當currentImage計數器達到6時,你會發生什麼? – 2011-05-06 05:39:40