Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Gif only plays once in JFrame, then stops

I'm relatively new to Java, and coding in general... so I would like to know what you guys make of this.

I'm making a dialog box for a game, and I want a little arrow gif to play while the dialog box is displaying text. The gif plays, but freezes after the first loop. I want it to play indefinitely.

The gif is cast into a JLabel like this...

arrow = new JLabel(new ImageIcon("arrow.gif"));
arrow.setBounds(800, 512, 100, 100);
panel.add(arrow); //JPanel created earlier in the method
arrow.setVisible(false);

and then immediately set invisible until it is ready to be used.

Here is my displayText method, in which the gif is used...

TextProcessor processor = new TextProcessor();
    processor.processText(str);
    processor.setLines();

    JButton button = new JButton();
    button.setBounds(0, 0, 1024, 768);
    panel.add(button);
    button.setBorderPainted(false);
    button.setOpaque(false);
    button.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                if(!processor.cycleLines())finishedWriting = true;
                else writeOnce = true;
            } 
        } );

    arrow.setVisible(true);

    l.setText("");
    l2.setText("");
    String line1 = processor.getLine1();
    String line2 = processor.getLine2();

    for(int i = 0; i < line1.length(); i++)
    {
        l.setText(l.getText() + line1.substring(i, i + 1));
        try{
            Thread.sleep(30);}catch(Exception e){}
    }
    for(int i = 0; i < line2.length(); i++)
    {
        l2.setText(l2.getText() + line2.substring(i, i + 1));
        try{
            Thread.sleep(30);}catch(Exception e){}
    }

    writeOnce = false;
    while(!finishedWriting){
        try{
            Thread.sleep(1);}catch(Exception e){}
        line1 = processor.getLine1();
        line2 = processor.getLine2();
        if(writeOnce)
        {   
            l.setText(line1);
            l2.setText("");
            for(int i = 0; i < line2.length(); i++)
            {
                l2.setText(l2.getText() + line2.substring(i, i + 1));
                try{
                    Thread.sleep(30);}catch(Exception e){}
            }
            writeOnce = false;
        }
    }
    arrow.setVisible(false);

Why isn't the gif repeating?

For clarification, the TextProcessor class divides up strings into 20-character segments. These segments are stored in an ArrayList, and are then retrieved by calling the TextProcessor's methods(getLine1, getLine2, cycleLines, setLines)

I would be happy to clarify anything that I left out but should have included.

Comments