0

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();
        }
    }
Hobbs2000
  • 311
  • 1
  • 6
  • 19
  • 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 – MadProgrammer Apr 08 '16 at 23:58
  • What is the error you're getting? Sidenote: there is a bug in "if (index > sprites.length)", as this allows index to be the equal to sprites.length, which when used to access the sprites in "sprites[index]" will lead to an ArrayIndexOutOfBoundsException. Should change it to "if (index == sprites.length)" – mmaarouf Apr 09 '16 at 01:41

1 Answers1

0

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366