(Java Swing) 我的动画线程没有并行运行

huangapple go评论66阅读模式
英文:

(Java Swing) My animating threads are not running parallely

问题

我在这段代码片段中的尝试是同时对两张图片进行移动(动画化)。然而,只有一张图片移动,另一张保持静止。通过使用打印命令进行检查,我发现第一个线程大多数时间是唯一一个在运行的线程,而在 for 循环中创建的第二个线程可能只运行了很短的一部分时间。我会非常感谢关于如何解决这个问题并使两个线程并行工作的帮助。

private static void createPan() {
    for (int i = 0; i < listSize; i++) {
        ImageList.add(new JLabel(images.getImage()));
        ImageList.get(i).setBounds(0, 0, 10, 10);

        List.add(new Motion(x, y));
        thrdList.add(new Thread(new threadFunc(i)));

        dispPane.add(ImageList.get(i));
        thrdList.get(i).start();
    }
}

private static class threadFunc implements Runnable {
    private static int indexNo;
    private static Timer t = new Timer(10, new AnimationListener(indexNo));

    public threadFunc(int index) {
        indexNo = index;
    }

    @Override
    public void run() {
        setLocation(indexNo);
        System.out.println("end");
    }

    private static void setLocation(int indexNo) {
        ImageList.get(indexNo).setBounds(List.get(indexNo).getX(), List.get(indexNo).getY(), 10, 10);
        t.start();
    }

    private static class AnimationListener implements ActionListener {
        private static int indexNo;

        public AnimationListener(int index) {
            indexNo = index;
        }

        public void actionPerformed(ActionEvent e) {
            List.get(indexNo).setX(List.get(indexNo).getX() + List.get(indexNo).getVelocityX());
            List.get(indexNo).setY(List.get(indexNo).getY() + List.get(indexNo).getVelocityY());
            setLocation(indexNo);
        }
    }
}

请注意,这是您提供的代码的翻译部分。如果您有任何问题或需要进一步的帮助,请随时提问。

英文:

What I am trying to do in this snippet of code is moving (animating) to images at the same time. However, only one image moves, and the other one just stays still. While I checked on this using print commands I found that the first thread was the only thread functioning most of the time while the second one created in the for loop functioned for maybe only a fraction of that time. I would really appreciate help on how to approach this problem and make both threads function parallelly.

private static void createPan() {
for(int i = 0; i &lt; listSize; i++) {
//System.out.println(&quot;=&quot;+i);
ImageList.add(new JLabel(images.getImage()));
ImageList.get(i).setBounds(0,0,10,10);
List.add(new Motion(x,y));
thrdList.add(new Thread(new threadFunc(i)));
//System.out.println(threadList.size());
dispPane.add(ImageList.get(i));
thrdList.get(i).start();
}
}
private static class threadFunc implements Runnable {
private static int indexNo;
private static Timer t = new Timer(10, new AnimationListener(indexNo));
public threadFunc(int index) {
indexNo = index;
}
@Override
public void run() {
//System.out.println(&quot;-&quot;+indexNo);
setLocation(indexNo);
System.out.println(&quot;end&quot;);
}
private static void setLocation(int indexNo) {
// System.out.println(&quot;+&quot;+indexNo);
//System.out.println(c + &quot;,&quot; + d);
//System.out.println(&quot;+&quot;+indexNo);
ImageList.get(indexNo).setBounds(List.get(indexNo).getX(), List.get(indexNo).getY(), 10, 10);
t.start();
}
private static class AnimationListener implements ActionListener {
private static int indexNo;
public AnimationListener(int index) {
indexNo = index;
}
public void actionPerformed(ActionEvent e) {
//System.out.println(indexNo);
//System.out.println(&quot;-&quot;+indexNo);
List.get(indexNo).setX(List.get(indexNo).getX()+List.get(indexNo).getVelocityX());
List.get(indexNo).setY(List.get(indexNo).getY()+List.get(indexNo).getVelocityY());
setLocation(indexNo);
}
}
}

答案1

得分: 1

private int indexNo;
private Timer t = new Timer(10, new AnimationListener(indexNo));

public threadFunc(int index) {
    indexNo = index;
}

private class AnimationListener implements ActionListener {
    private int indexNo;

    public AnimationListener(int index) {
        indexNo = index;
    }
}

Don't use static variables. 

A static variable means it is shared by all instances of the class. So when you create the second instance of the same class, the value of the variable is reset to the value of the last parameter. So you only get animation on a single object.

However, I would also agree with the suggestion to use a single Timer. In this design, you keep an ArrayList of objects you want to animate and then iterate through the ArrayList to reset the location of each object.

See: [https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681](https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681) for a working example. The example can handle hundreds of moving objects. It would not be practical to have hundreds of individual Timers animating each object separately.
英文:
private static int indexNo;
private static Timer t = new Timer(10, new AnimationListener(indexNo));
public threadFunc(int index) {
indexNo = index;
}

and

private static class AnimationListener implements ActionListener {
private static int indexNo;
public AnimationListener(int index) {
indexNo = index;
}

Don't use static variables.

A static variable means it is shared by all instances of the class. So when you create the second instance of the same class the value of the variable is reset to the value of the last parameter. So you only get animation on a single object.

However, I would also agree with the suggestion to use a single Timer. In this design you keep an ArrayList of objects you want to animate and then iterate through the ArrayList to reset the location of each object.

See: https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681 for a working example. The example can handle hundreds of moving objects. It would not be practical to have hundreds of individual Timers animating each object separately.

huangapple
  • 本文由 发表于 2020年8月21日 12:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63516508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定