Java 如何在ImageIcon中模拟视频?
我使用JToggleButton和此方法捕获事件:Java 如何在ImageIcon中模拟视频?,java,multithreading,swing,video,imageicon,Java,Multithreading,Swing,Video,Imageicon,我使用JToggleButton和此方法捕获事件: tgl_playMouseClicked(java.awt.event.MouseEvent evt) { new Thread() { public void run() { int i = 0; String outp
tgl_playMouseClicked(java.awt.event.MouseEvent evt) {
new Thread() {
public void run() {
int i = 0;
String outputName = null;
while ((i <= 99)) {
ImageIcon imgThisImg = new ImageIcon("images/" + outputName + i + ".png");
lbl_image.setIcon(imgThisImg);
i++;
}
tgl_play.setSelected(!tgl_play.isSelected());
}
}.start();
}
tgl_playMouseClicked(java.awt.event.MouseEvent evt){
新线程(){
公开募捐{
int i=0;
字符串outputName=null;
while((i我认为您最好的方法之一是使用javax.swing.Timer
调整“视频”的速度。这将确保您使用swing EDT正确地完成所有操作
(如果毫秒不够,那么我将看一看:java.util.concurrent.Executors.newScheduledThreadPool(int)
和java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(Runnable,long,long,TimeUnit)
并添加Runnable
,它们会立即调用SwingUtilities.invokeLater()中的所有代码
在这里,我制作了一个小的演示示例,其中显示了一个不断增长和缩小的圆圈的图像列表(这些图像是使用一些JPanel动态创建的,但这只是为了演示)
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.GraphicsEnvironment;
导入java.awt.Image;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.Timer;
导入javax.swing.UnsupportedLookAndFeelException;
公开类测试{
私有静态最终int NB_图像=50;
每秒图像的私有静态最终int NB_=25;
专用静态最终整数宽度=300;
专用静态最终内部高度=300;
受保护的void initUI(){
final JFrame=newjframe(TestAnimation.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(false);
//我将在其上设置图像的标签
最终JLabel标签=新的JLabel();
//通过添加到框架,它被设置为框架的中心构件
//JFrame的BorderLayout。最终,标签将具有内容窗格的大小
框架。添加(标签);
框架尺寸(宽度、高度);
//创建图像列表(仅用于演示目的)
最终列表图像=新的阵列列表(NB_图像);
对于(int i=0;i
我认为您最好的方法之一是使用javax.swing.Timer
来调整“视频”的速度。这将确保您使用swing EDT正确完成所有工作
(如果毫秒不够,那么我将看一看:java.util.concurrent.Executors.newScheduledThreadPool(int)
和java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(Runnable,long,long,TimeUnit)
并添加Runnable
,它们会立即调用SwingUtilities.invokeLater()中的所有代码
在这里,我制作了一个小的演示示例,其中显示了一个不断增长和缩小的圆圈的图像列表(这些图像是使用一些JPanel动态创建的,但这只是为了演示)
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.GraphicsEnvironment;
导入java.awt.Image;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.Timer;
导入javax.swing.UnsupportedLookAndFeelException;
公开类测试{
私有静态最终int NB_图像=50;
每秒图像的私有静态最终int NB_=25;
专用静态最终整数宽度=300;
专用静态最终内部高度=300;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UnsupportedLookAndFeelException;
public class TestAnimation {
private static final int NB_OF_IMAGES = 50;
private static final int NB_OF_IMAGES_PER_SECOND = 25;
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
protected void initUI() {
final JFrame frame = new JFrame(TestAnimation.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
// the label on which I will set images
final JLabel label = new JLabel();
// By adding to the frame, it is set as the central component of the
// BorderLayout of the JFrame. Eventually, the label will have the size of the content pane
frame.add(label);
frame.setSize(WIDTH, HEIGHT);
// Creating a list of images (just for demo purposes)
final List<Image> images = new ArrayList<Image>(NB_OF_IMAGES);
for (int i = 0; i < NB_OF_IMAGES; i++) {
CirclePanel circle = new CirclePanel(WIDTH / 2, WIDTH / 2, 2 * WIDTH * (NB_OF_IMAGES / 2 - Math.abs(i - NB_OF_IMAGES / 2))
/ NB_OF_IMAGES);
circle.setSize(WIDTH, HEIGHT);
BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
.createCompatibleImage(WIDTH, HEIGHT, BufferedImage.TRANSLUCENT);
circle.print(image.getGraphics());
images.add(image);
}
// Here is the timer logic
Timer t = new Timer(1000 / NB_OF_IMAGES_PER_SECOND, new ActionListener() {
private int i = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (i == images.size()) {
i = 0;
}
label.setIcon(new ImageIcon(images.get(i++)));
}
});
frame.setVisible(true);
t.start();
}
// Simple class that draws a red circle centered on x,y and given radius
public static class CirclePanel extends JPanel {
private int x;
private int y;
private int radius;
public CirclePanel(int x, int y, int radius) {
super();
this.x = x;
this.y = y;
this.radius = radius;
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawArc(x - radius / 2, y - radius / 2, radius, radius, 0, 360);
}
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestAnimation().initUI();
}
});
}
}