简体   繁体   English

新线程无法与JFrame一起使用

[英]New Thread not working with JFrame

I have made a class that displays a series of images to create an animation. 我做了一个显示一系列图像以创建动画的类。 I would like the animation to take place on a separate thread than the rest of the program. 我希望动画发生在与程序其余部分不同的线程上。 However, when I try to start the animation class, I get an error. 但是,当我尝试启动动画类时,出现错误。

This is some of the animation class (There is more but is is irrelevant) : 这是一些动画类 (还有更多但并不相关)

    /**
     * Starts a new thread to play the animation on
     */
    public void start()
    {
        playing = true;
        animateThread = (new Thread(() -> run()));
        animateThread.start();
    }

    /**
     * Will go through sprites and display the images
     */
    public void run()
    {
        int index = 0;
        while (playing)
        {
            if (index > sprites.length)
            {
                index = 0;
            }
            try
            {
                g.drawImage(sprites[index].getImage(), x, y, null);
                animateThread.sleep(speed);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            index++;
        }
    }

I have also attempted at making the animation class Runnable, then making the whole object a new Thread but I received the same error. 我也尝试过使动画类Runnable,然后使整个对象成为新的Thread,但是我收到了相同的错误。

This is the class which holds the JFrame and starts the animation (There is more but is is irrelevant) : 这是保存JFrame并启动动画的类 (还有更多但不相关)

 public static void main(String[] args)
    {
        AnimationTester tester = new AnimationTester();
        tester.frame.setResizable(false);
        tester.frame.setTitle("Tester");
        tester.frame.add(tester);
        tester.frame.pack();

        tester.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        tester.frame.setLocationRelativeTo(null);
        tester.frame.setVisible(true);

        //Make the window not have to be clicked on to get input (Set is as the main focus when it begins)
        tester.requestFocusInWindow();

        //Start the program
        tester.start();
    }

    public void start()
    {
        createGraphics();
        animation.start();
    }


    public void createGraphics()
    {
        BufferStrategy bs = getBufferStrategy();
        //Checks to see if the BufferStrategy has already been created, it only needs to be created once
        if (bs == null)
        {
            //Always do triple buffering (put 3 in the param)
            createBufferStrategy(3);
            return;
        }


        //Links the bufferStrategy and graphics, creating a graphics context
        g = bs.getDrawGraphics();

        try
        {
            animation = new Animation(ImageIO.read(getClass().getResource("/SpriteSheet.jpg")), 16, 2, 200, 250, 250, 2.0);
            animation.addGraphics(g);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

That's not really how BufferStrategy works, instead of using createGraphics , you should be calling it to update the state and render it to the screen 这不是BufferStrategy的真正工作方式,而不是使用createGraphics ,应该调用它来更新状态并将其呈现到屏幕上

So, in your Thread , it should be updating the state in some way and call some "render" method which would get the next page, render the state and push that state to the screen. 因此,在您的Thread ,它应该以某种方式更新状态并调用某种“ render”方法,该方法将获取下一页,呈现状态并将该状态推送到屏幕。

Take a closer look at BufferStrategy and BufferStrategy and BufferCapabilities for more details about how it's suppose to work 仔细研究BufferStrategyBufferStrategy和BufferCapabilities,以获取有关如何工作的更多详细信息

For example 举个例子

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

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