[英]Problems repainting in a JPanel Runnable
I am writing a program that will play a song and have a JPanel displaying images during it. 我正在编写一个程序,该程序将播放歌曲并在其间显示图像的JPanel。 The song plays fine, the first image is drawn (I assume from the initial call to paintComponent) but somehow the repaint() doesn't seem to be getting called.
这首歌播放得很好,绘制了第一张图像(我假设是从对paintComponent的初始调用中得出的),但是某种程度上似乎并未调用repaint()。 I could really use an extra set of eyes.
我真的可以多用一些眼睛。 I have the code below for the JPanel class that will be displaying the images.
我在下面的JPanel类代码中将显示图像。 Thanks so much!
非常感谢!
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);
}
}
imagerunner
. imagerunner
。 The waiting()
method seems to be blocking the EDT. waiting()
方法似乎正在阻止EDT。 It would be better to use a Swing Timer
to schedule the updates. 最好使用Swing
Timer
来计划更新。
You would need to do the following: 您需要执行以下操作:
1) Actually create an instance to run. 1)实际创建一个要运行的实例。 2) You will need to invoke repaint() regularly in order to get your display to repaint.
2)您需要定期调用repaint()才能使您的显示重新绘制。
Hope it helps. 希望能帮助到你。 Cheers!
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.