Java 使用Swing定时器在GUI上更新BuffereImage
我一直在尝试使用Swing计时器更新插入GUI的BuffereImage。 最初,我使用通过控制器实例获得的BuffereImage创建ImageIcon,然后将其添加到JLabel(字段)。当前图像随后显示在GUI上。 这里是GUI类中初始代码的代码片段Java 使用Swing定时器在GUI上更新BuffereImage,java,swing,user-interface,Java,Swing,User Interface,我一直在尝试使用Swing计时器更新插入GUI的BuffereImage。 最初,我使用通过控制器实例获得的BuffereImage创建ImageIcon,然后将其添加到JLabel(字段)。当前图像随后显示在GUI上。 这里是GUI类中初始代码的代码片段 JLabel field; ImageIcon icon; BufferedImage image = this.controller.getCameraImage(); icon = new ImageIcon(image); field=
JLabel field;
ImageIcon icon;
BufferedImage image = this.controller.getCameraImage();
icon = new ImageIcon(image);
field= new JLabel (icon);
现在我尝试每x秒获取一个BuffereImage。
为此,我使用了Swing Timer类,但不幸的是,我没有成功。
我用ActionListener创建了下面的类,这样每3秒钟就会用getCameraImage方法检索一个新图像
这里是带有ActionListener类的UpdateImage:
public class UpdateImage {
private JLabel label;
private Timer timer;
private Controller controller;
private int delay = 3000; // every 3 seconds
private ImageIcon icon;
private BufferedImage image;
public UpdateImage(Controller controller) throws Exception {
this.controller = controller;
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
try {
System.out.println("image update");
label = new JLabel();
image = controller.getCameraImage();
label.setIcon(new ImageIcon(image));
} catch (CallError | InterruptedException e) {
e.printStackTrace();
}
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start();
}
public JLabel getLabel() {
return label;
}
}
将BuffereImage设置为ImageIcon后,我使用getLabel方法检索它。
然后将此组件添加到GUI
public class GUI{
UpdateImage update;
//more code
JLabel field;
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
try {
new UpdateImage(controller);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
field = update.getLabel( );
//more code
}
字符串“image update”(图像更新)在控制台上按需要每3秒显示一次。
但是图像不会显示在GUI上
如果有人能给我一些提示或指出我做错了什么,我将不胜感激。当您使用具有相同文件名的ImageIcon时,图像将被缓存。您需要强制重新加载映像:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import java.net.*;
public class ImageReload extends JPanel implements ActionListener
{
JLabel timeLabel;
JLabel imageLabel;
ImageIcon icon = new ImageIcon("timeLabel.jpg");
public ImageReload()
{
setLayout( new BorderLayout() );
timeLabel = new JLabel( new Date().toString() );
imageLabel = new JLabel( timeLabel.getText() );
add(timeLabel, BorderLayout.NORTH);
add(imageLabel, BorderLayout.SOUTH);
javax.swing.Timer timer = new javax.swing.Timer(1000, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
timeLabel.setText( new Date().toString() );
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
String imageName = "timeLabel.jpg";
BufferedImage image = ScreenImage.createImage(timeLabel);
ScreenImage.writeImage(image, imageName);
// This works using ImageIO
imageLabel.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );
// Or you can flush the image
/*
ImageIcon icon = new ImageIcon(imageName);
icon.getImage().flush();
imageLabel.setIcon( icon );
*/
imageLabel.setText("");
}
catch(Exception e)
{
System.out.println( e );
}
}
});
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ImageReload() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
编辑:
您还需要该类来执行此代码。交叉发布:。这正是我要找的。我通读了代码,它真的帮了我的忙。非常感谢。最后一个问题。时间标签是必要的吗?我在没有使用它的情况下测试了它,GUI甚至不会弹出。很抱歉,我忘了包含指向
ScreenImage
类的链接(请参阅我刚才添加的编辑链接)。时间标签每秒更新一次,因此我使用ScreenImage
类创建一个新图像,将图像保存到文件中,然后重新加载图像文件。如果从该代码中删除标签,则该类甚至不会编译。我不知道为什么你的GUI不会弹出。即使框架不包含任何组件,也可以显示框架。