jpanel dont draw a buffered image but the buffered image can be created as file
-
28-10-2019 - |
题
Hi i have a jpanel to draw on. from this jpanel i make bufferedimages and safe them into a linkedlist. on a button press i want to animate this bufferedimages(play one after one) the problem is that the jpanel dont show the buffered images but when i use ImageIO.write to safe the bufferedimages on disk i get all the pictures i want to animate. pls help me.
here is my code:
public void run(){
for(int i=0;i`<`cm.animationListe.size();i++){
b= cm.animationListe.get(i);
try {
ImageIO.write( b, "png", new File( "c:/java/circle"+i+".png" ) );
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(1000);
repaint();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
animation = false;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(animation){
g.drawImage(b,0, 0,null);
}
}
解决方案
The Thread.sleep() causes the GUI to freeze so it can't repaint itself.
To do animation you need to use a Swing Timer.
不隶属于 StackOverflow