0

I have a problem, creating my Java frame application. I can't call repaint of component in a loop; This is the part of mo class:

if(e.getSource()==drop){
    //check if the row has space
    if(currentGame.isFull(choosePosition)){
        return; 
        }
    else{
        int row = currentGame.placeFigure(choosePosition, turn);

        ImageIcon temp;
        if(turn)
            temp = firstIcon;
        else
            temp = secondIcon;
        for(int i = 0; i != row + 1; ++i){
            cells[i][choosePosition].setIcon(temp);
            if(i != 0)
                cells[i - 1][choosePosition].setIcon(emptyIcon);
            gameBoardPanel.repaint();
            gameBoardPanel.revalidate();
            Graphics myGraphics = getGraphics();
            // Draw as appropriate using myGraphics
            myGraphics.dispose();
            paint(myGraphics);
            try {
                  Thread.sleep(500);
                } catch (InterruptedException ie) {
                    //Handle exception
                    System.out.println("can't start animation");
                }
        }
        repaint();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3014909
  • 41
  • 2
  • 12
  • 1
    Don't create an animation in a frame but instead a `JPanel` or `BufferedImage`. 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Nov 20 '13 at 20:47
  • 1
    Don't block the event dispatching thread. Instead use javax.swing.Timer – MadProgrammer Nov 20 '13 at 20:49
  • 1
    Also, don't use getGraphics (and don't dispose of it if you do), this is not how painting is done. Take a look at [Performing custom painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details – MadProgrammer Nov 20 '13 at 20:52
  • I realized it using sleep :) – user3014909 Dec 09 '13 at 23:01
  • Is my question not working for you? – Jont Jun 24 '14 at 07:24

1 Answers1

-1

Start learning how to use the paintComponent method. And here's how to use Timers:

public class your_class_name extends if_you_need_to_extend implments ActionListener
{

//In the constructor...

    Timer t = new Timer(milliseconds,this);
    t.start();

//In the main class...

@Override
public void actionPerformed(ActionEvent e)
{
    //animations here
    repaint(); //calls paintComponent again
}

@Override
public void paintComponent(Graphics g)
{
    //default drawings here
}
}
Jont
  • 194
  • 14