繁体   English   中英

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

[英]Java: drawing multiple sprites in different threads at a time

我需要创建一个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();
    }
}

类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);
        }
    }
}

类精灵:

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

这段代码创建了一个新的移动精灵。 但是,先前的精灵会从面板中删除。 我需要做的是用鼠标单击添加一个新的精灵,以便不删除以前的精灵。 如果单击三次,则应绘制三个精灵。

如何更改我的代码以实现该目标?

非常感谢!

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

暂无
暂无

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

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