简体   繁体   中英

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.

This is the class which holds the JFrame and starts the animation (There is more but is is irrelevant) :

 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

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.

Take a closer look at BufferStrategy and BufferStrategy and BufferCapabilities for more details about how it's suppose to work

For example

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