Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(多图像)动画Java_Java_Swing_User Interface - Fatal编程技术网

(多图像)动画Java

(多图像)动画Java,java,swing,user-interface,Java,Swing,User Interface,我的程序中有一组节点,每个节点都有一个特定的x,y位置。 每个都有一组图像图标 我想在每个节点的特定位置绘制图像动画 这是我的代码:这只显示了我知道原因的最后一张图片 public void showPicture() { //nodes : for(int i=0;i<thisGraph.getNode().size();i++){ if(thisGraph.getNode().get(i).getImageIcon()!=(n

我的程序中有一组节点,每个节点都有一个特定的x,y位置。 每个都有一组图像图标

我想在每个节点的特定位置绘制图像动画

这是我的代码:这只显示了我知道原因的最后一张图片

public void showPicture()  {
        //nodes : 
        for(int i=0;i<thisGraph.getNode().size();i++){
            if(thisGraph.getNode().get(i).getImageIcon()!=(null)){
                for(int j=0;j<thisGraph.getNode().get(i).getImageIcon().size();j++){
                    if(j>0)
                       lables.get(lables.size()-1).setVisible(false);
                    JLabel jLabel1 = new JLabel();
                    lables.add(jLabel1);
                    jLabel1.setLayout(new GridBagLayout());
                    jLabel1.setIcon(thisGraph.getNode().get(i).getImageIcon().get(j));
                    jLabel1.setVisible(true);
                    jLabel1.setBounds((int)thisGraph.getNode().get(i).getX(),(int)thisGraph.getNode().get(i).getY(),195,163);           
                    jPanel1.add(jLabel1);

                }
            }
        }
    }

Swing是单线程的,不是线程安全的。这意味着您不应该使用长时间运行或阻塞操作(如Thread.sleep)来阻塞事件调度线程。您还应该仅从事件调度线程的上下文中更新UI或它所依赖的任何内容

有关更多详细信息,请参阅

可能解决问题的最简单方法是使用摆动计时器

其思想是使用单个计时器作为主动画循环,更改所有需要更新的对象的属性

下面是一个非常基本的示例,它为100个JLabel设置动画,只需使用随机选择的颜色更改它们的背景色


有关更多详细信息,请参见

我尝试的方法:Thread.sleep->它会冻结按钮。由于在论坛中已经讨论过多次,Thread.sleep会冻结应用程序,因为它会停止EDT。您需要使用摆动计时器,为什么它只显示最后一张图片?因为showPicture方法将运行所有for循环,只显示最后一个循环。为了纠正这一点,一个摆动计时器也会有所帮助,这是另一个与您遇到的问题类似的例子。如果在阅读了这两个例子并进行了调查后,您仍然无法解决问题,请发布一个正确的解决方案,并在阅读上述示例后进行了改进。我阅读了这些示例,编辑了我的文本。请再看一次@frakcool你读过关于?你不明白哪一部分?你的例子还远远不够完整。但它仍然是冻结按钮,不起作用,你仍然在里面使用for循环和while循环。你启动计时器,但在计时器启动后立即停止。。
int j = 0;
    public void showPicture(){
        //nodes : 
        for(int i=0;i<thisGraph.getNode().size();i++){
            if(thisGraph.getNode().get(i).getImageIcon()!=(null)){
              j=0;
                while( j<thisGraph.getNode().get(i).getImageIcon().size()){

                    if(j>0)
                        lables.get(lables.size()-1).setVisible(false);
                    JLabel jLabel1 = new JLabel();
                    lables.add(jLabel1);
                    jLabel1.setLayout(new GridBagLayout());
                    jLabel1.setIcon(thisGraph.getNode().get(i).getImageIcon().get(j));
                    jLabel1.setVisible(true);
                    jLabel1.setBounds((int)thisGraph.getNode().get(i).getX(),(int)thisGraph.getNode().get(i).getY(),195,163);

                    jPanel1.add(jLabel1);

                    //

                    ActionListener act;
                    act = new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            jLabel1.setVisible(true);        
                            j++;
                        }
                    };
                    Timer timer = new Timer(1000, act );
                    timer.start();

                    timer.stop();
                    //

                }
            }
        }}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<JLabel> nodes = new ArrayList<>(100);

        private Random random = new Random();
        private Color[] colors = new Color[] { Color.RED, Color.GREEN, Color.BLUE, Color.BLACK, Color.MAGENTA};

        public TestPane() {
            setLayout(new GridLayout(0, 10));
            for (int index = 0; index < 100; index++) {
                JLabel label = new JLabel();
                label.setBorder(new EmptyBorder(5, 5, 5, 5));
                label.setOpaque(true);
                label.setBackground(pickColor());
                nodes.add(label);
                add(label);
            }
            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (JLabel label : nodes) {
                        label.setBackground(pickColor());
                    }
                }
            });
            timer.start();
        }

        protected Color pickColor() {
            return colors[random.nextInt(colors.length)];
        }

    }

}