jpanel不绘制缓冲图像,但缓冲图像可以创建为文件
嗨,我有一个 jpanel 可以用来画画。从这个 jpanel 中,我制作了缓冲图像并将它们安全地放入链接列表中。按下按钮后,我想为该缓冲图像设置动画(一张接一张地播放) 问题是 jpanel 不显示缓冲图像,但是当我使用 ImageIO.write 来保护磁盘上的缓冲图像时,我得到了我想要制作动画的所有图片。请帮助我。
这是我的代码:
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Thread.sleep() 会导致 GUI 冻结,因此无法重新绘制自身。
要制作动画,您需要使用 Swing 计时器。
The Thread.sleep() causes the GUI to freeze so it can't repaint itself.
To do animation you need to use a Swing Timer.