Java:一次在不同的线程中绘制多个精灵

时间:2016-09-14 00:27:05

标签: java multithreading jpanel sprite

我需要创建一个JPanel,在鼠标单击时,新的Sprite必须出现在新线程中。这就是我所拥有的:

public class BouncingSprites {

    private JFrame frame;
    private SpritePanel panel = new SpritePanel();
    private Sprite ball;

    public BouncingSprites() {
        frame = new JFrame("Bouncing Sprite");
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setVisible(true);
    }
    public void start(){
        panel.animate();  // never returns due to infinite loop in animate method
    }

    public static void main(String[] args) {
        new BouncingSprites().start();
    }
}

Class SpritePanel:

public class SpritePanel extends JPanel
{
    Sprite sprite;

    public SpritePanel()
    {
       addMouseListener(new Mouse());
    }

    //method for creating a new ball
    private void newSprite (MouseEvent event)
    {
        sprite = new Sprite(this);
    }

    public void animate()
    {
    }

    private class Mouse extends MouseAdapter 
    {
        @Override
        public void mousePressed( final MouseEvent event )
        {
            newSprite(event);
        }
    }


    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if (sprite != null)
        {
            sprite.draw(g);
        }
    }
}

Class Sprite:

public class Sprite implements Runnable
{
    public final static Random random = new Random();
    final static int SIZE = 10;
    final static int MAX_SPEED = 5;
    SpritePanel panel;
    private int x;
    private int y;
    private int dx;
    private int dy;
    private Color color = Color.BLUE;
    private Thread animation;

    public Sprite (SpritePanel panel)
    {
        this.panel = panel;
        x = random.nextInt(panel.getWidth());
        y = random.nextInt(panel.getHeight());
        dx = random.nextInt(2*MAX_SPEED) - MAX_SPEED;
        dy = random.nextInt(2*MAX_SPEED) - MAX_SPEED;

        animation = new Thread(this);
        animation.start();
    }

    public void draw(Graphics g)
    {
        g.setColor(color);
        g.fillOval(x, y, SIZE, SIZE);
    }

    public void move()
    {

        // check for bounce and make the ball bounce if necessary
        //
        if (x < 0 && dx < 0){
            //bounce off the left wall 
            x = 0;
            dx = -dx;
        }
        if (y < 0 && dy < 0){
            //bounce off the top wall
            y = 0;
            dy = -dy;
        }
        if (x > panel.getWidth() - SIZE && dx > 0){
            //bounce off the right wall
            x = panel.getWidth() - SIZE;
            dx = - dx;
        }       
        if (y > panel.getHeight() - SIZE && dy > 0){
            //bounce off the bottom wall
            y = panel.getHeight() - SIZE;
            dy = -dy;
        }

        //make the ball move
        x += dx;
        y += dy;
    }

    @Override
    public void run() 
    {
        while (Thread.currentThread() == animation) 
        {
            move(); 
            panel.repaint();

            try 
            {
                Thread.sleep(40);
            }
            catch ( InterruptedException exception ) 
            {
                exception.printStackTrace();
            }   
        }
    }
}

此代码创建一个新的移动精灵。但是,之前的精灵会从面板中删除。我需要做的是通过鼠标单击添加一个新的精灵,以便不删除前一个精灵。如果我点击三次,应该画三个精灵。

如何更改我的代码以实现它?

非常感谢!

1 个答案:

答案 0 :(得分:1)

您可以使用Sprite sprite;替换SpritePanel课程中的List<Sprite> sprites = new ArrayList<>();。然后,当您创建新的Sprite时,将其添加到列表中。然后在paintComponent方法中,您可以遍历列表,绘制所有精灵。