0

I'm trying to animate an object using the swing timer. I need the object to change its y coordinate each time until it reaches the bottom. With the code below, how could I paint the animated piece through it's animation, before painting all of the pieces in the list within the for loop?

            int x = (getMouseX()) * 70 + 10;
            int y = mover.getRow() * 70 + 10;
            pieceList.add(new Oval(x, y, 50, 50, setPieceColor()));
            g.setColor(setPieceColor());


            // **** Animate new piece being dropped here ****


            for (int i = 0; i < pieceList.size(); i++) {
                g.setColor(pieceList.get(i).getColor());
                g.fillOval((int) pieceList.get(i).getX(), (int) pieceList.get(i).getY(), (int) pieceList.get(i).getWidth(), (int) pieceList.get(i).getHeight());
            }
           mover.previewMove(g2d, getCurrentColumn());

My issue with animating the piece is that I'm unsure how to do it so that it isn't painted in it's final place before it has dropped. Since I'm adding each piece to a list as it's played, the list of pieces are all being painted after every click / mouse movement. How could I animate the dropping piece before it is added into the list to be drawn on each of the following turns?

AlecR
  • 57
  • 7
  • 3
    So where is your Timer code?. Create a simple demo (called a `SSCCE`) that just uses a Timer and does the animation. Once you figure out how that process works, you then add that code to your real application. Paint code simply paints the current state of your application. The point of the Timer is to change the state of 1 (or more) objects. If your Timer code only changes the location of the object you want to animate, then it doesn't matter if you also paint the other objects because they will remain in the same location. – camickr Apr 23 '15 at 21:05
  • 1
    For [example](http://stackoverflow.com/a/11233735/230513). – trashgod Apr 24 '15 at 01:10

0 Answers0