Results 1 to 4 of 4
  1. #1
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    63
    Rep Power
    0

    Default How to stop timer-based animation when it completes

    I'm making a version of the 15-puzzle (aka the Tiles Game) in Java. Rather than using JButtons, I decided to just paint it onto a JPanel, because I would like tiles to slide to the empty place when clicked, rather than just move there instantly, and I'm not sure how I would go about doing that with JButtons. I've checked out a few tutorials on animation using Swing Timers, but unfortunately they only covered perpetual motion, or stopping the timer manually.

    The problem I'm facing is that I have to somehow initialize a Timer with an ActionListener that references this same Timer. This is my move method currently, which is supposed to slide a tile into the empty space (it assumes that the move is valid, which is checked in the mouse and key listeners that call move):
    Java Code:
    // move the piece currently in position [i][j] in direction dir (which is a swing constant - NORTH, SOUTH, EAST, WEST)
    
    private void move (final int i, final int j, final int dir) {
    
         // calculate destinations for x and y coordinate based on dir
         // (only one coordinate changes during the course of a single move)
        
    
        final Timer timer = new Timer (20, new ActionListener() {
            @Override
            public void actionPerformed (ActionEvent e) {
                if (x[i][j] == destX && y[i][j] == destY) timer.stop();      // if the tile has reached its destination, stop the animation
                else step (i, j, dir);                                       // else perform a step in the direction of the move
            }
        });
        timer.start();
    }
    
    private void step (int i, int j, int dir) {
        switch (dir) {
            case SwingConstants.NORTH:
                y[i][j]--;
                break;
            case SwingConstants.SOUTH:
                y[i][j]++;
                break;
            case SwingConstants.WEST:
                x[i][j]--;
                break;
            case SwingConstants.EAST:
                x[i][j]++;
                break;
        }
        repaint();
    }
    Of course this gives me the error message that timer may not have been initialized in the ActionListener. How would I properly do this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: How to stop timer-based animation when it completes

    Get a reference to the Timer object from the event object.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: How to stop timer-based animation when it completes

    Quote Originally Posted by Mate de Vita View Post
    The problem I'm facing is that I have to somehow initialize a Timer with an ActionListener that references this same Timer.
    There are a variety of different solutions to your overall game. However, to address the specific problem, you can do this.

    Java Code:
    public void actionPerformed(ActionEvent ae) {
       Object obj = ae.getSource(); // source of the event.
       if (obj instanceof Timer) { // don't need this check if listener is devoted to Timer instances
           Timer timer = (Timer)obj;
           // do something with timer
       }
    }
    Does this help?

    Regards,
    Jim
    Last edited by jim829; 03-31-2014 at 05:17 PM. Reason: removed space in instance of -- instanceof
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    63
    Rep Power
    0

    Default Re: How to stop timer-based animation when it completes

    OK, that works, and it doesn't even give me an unsafe typecast warning, which I kind of expected, even if I don't do the instance of check.
    Thank you both!

Similar Threads

  1. how to stop a timer at particular time
    By Mr.cool in forum New To Java
    Replies: 2
    Last Post: 08-03-2012, 10:42 AM
  2. Help with a stop and continue button with a timer plz. JAVA
    By elbert.dewet@yahoo.com in forum AWT / Swing
    Replies: 1
    Last Post: 06-11-2012, 07:46 PM
  3. Stop a timer in other class
    By warchieflll in forum Advanced Java
    Replies: 15
    Last Post: 02-02-2011, 08:13 PM
  4. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 09:06 AM
  5. MouseClicked Stop Animation
    By xpngamer in forum New To Java
    Replies: 1
    Last Post: 04-14-2009, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •