[英]Using JFrame to display an image through a loop
我有一个递归循环,它对图像执行计算,并希望通过每次迭代显示图像的进度。
这就是我所拥有的:
static JFrame colFrame = new JFrame();
main() {}
loop() {
JLabel label = null;
ImageIcon colIcon = new ImageIcon(blockImg);
label = new JLabel(colIcon);
colFrame.getContentPane().add(label);
colFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close canvas once the window is closed
colFrame.pack();
colFrame.setVisible(true);
}
有谁知道如何更改我的代码,以便它将通过每次迭代显示图像?
使用Swing Timer计划更改图像的动画。
这是我每秒更改标签文本的简单示例:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class TimerTime extends JPanel implements ActionListener
{
private JLabel timeLabel;
private int count = 0;
public TimerTime()
{
timeLabel = new JLabel( new Date().toString() );
add( timeLabel );
Timer timer = new Timer(1000, this);
timer.setInitialDelay(1);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.getSource());
timeLabel.setText( new Date().toString() );
// timeLabel.setText( String.valueOf(System.currentTimeMillis() ) );
count++;
System.out.println(count);
if (count == 10)
{
Timer timer = (Timer)e.getSource();
timer.stop();
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("TimerTime");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TimerTime() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
在您的情况下,您可能想要更改Icon
(不创建新标签)。
因为您的算法是递归的,所以在SwingWorker
的doInBackground()
实现中调用它。 在每个级别, publish()
表示当前状态的BufferedImage
,并使用label.setIcon()
process()
它label.setIcon()
。 其产生的示例BufferedImage
示出这里 ,和产生一个相关的示例TexturePaint
示出此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.