简体   繁体   中英

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:

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:

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,

 @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());
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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