简体   繁体   English

如何在多圈动画中使用多线程?

[英]How to use multiple thread for multiple circle animation?

I already paint multiple circles and I want them to move to right simultaneously with different threads. 我已经绘制了多个圆圈,并且希望它们使用不同的线程同时向右移动。 So I have to use multiple threads. 所以我必须使用多个线程。 However, when I run the program, it repaints circles again and again. 但是,当我运行该程序时,它会一次又一次地重新绘制圆圈。 I actually know where the problem is but I don't know how to solve it. 我实际上知道问题出在哪里,但我不知道如何解决。 Here is my code: 这是我的代码:

Circles.java: Circles.java:

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;



public class Circles extends JPanel {
    private int x,y,r,s;
    private Color c;
    private Random ran;

    public Circles() {
        // TODO Auto-generated constructor stub
        ran = new Random();
        x = ran.nextInt(600)+5;
        y = ran.nextInt(600)+5;
        r = ran.nextInt(100)+20;
        s = ran.nextInt(10)+5;
        c = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255));
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getR() {
        return r;
    }

    public Color getC() {
        return c;
    }
    public int getS() {
        return s;
    }

    public void move(int speed) {
        x = x+speed;
    }
}

Game.java: Game.java:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;

public class Game extends JFrame implements Runnable {
    private static Circles myCircle;
    private static int numberOfCircle;
    private Random ran;
    private static ArrayList<Circles> circleList = new ArrayList<>();

    public Game() {
        // TODO Auto-generated constructor stub

        setLayout(new BorderLayout());
        setVisible(true);
        setSize(700,700);
        setTitle("Game");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Thread t = new Thread(this);
        t.start();
        System.out.println(t.getName());
    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        for(int i=0; i<numberOfCircle; i++) {
            myCircle = new Circles();
            g.setColor(myCircle.getC());
            g.fillOval(myCircle.getX(), myCircle.getY(), myCircle.getR(), myCircle.getR());
            circleList.add(myCircle);
        }

    }

    public static void main(String[] args) {
        Game game = new Game(); 
        numberOfCircle = Integer.parseInt(args[0]);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true) {
            try {
                Thread.currentThread().sleep(100);
                for(int i=0; i<numberOfCircle; i++) {
                    myCircle.move(circleList.get(i).getS());
                }
                repaint();
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println(e.getMessage());
            }
        }

    }

}

Any suggestion? 有什么建议吗?

Every time you are calling paint, you are creating new circles inside of paint method again and again, carry out your initialization outside of paint to some init method, 每次您调用paint时,您都会在paint方法内一次又一次地创建新的圆,在paint之外对某个init方法进行初始化,

 @Override
public void paint(Graphics g) {

    super.paint(g);
    for (Circles myCircle:circleList
         ) {
        g.setColor(myCircle.getC());
        g.fillOval(myCircle.getX(), myCircle.getY(), myCircle.getR(), myCircle.getR());
    }
}

public static void main(String[] args) {
    Game game = new Game(); 
    numberOfCircle = 5;

    for(int i=0; i<numberOfCircle; i++) {
        myCircle = new Circles();
        circleList.add(myCircle);
    }

}

@Override
public void run() {
    while(true) {
        try {
            Thread.currentThread().sleep(100);
            for (Circles myCircle:circleList
                 ) {
                myCircle.move(myCircle.getS());
            }
            repaint();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM