001package jmri.jmrit.operations.trains;
002
003import jmri.jmrit.operations.routes.RouteLocation;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/**
008 * Provides simple animation for train icons.
009 *
010 * @author Daniel Boudreau (C) Copyright 2009, 2010
011 */
012public class TrainIconAnimation extends Thread {
013
014    TrainIcon _trainIcon;
015    RouteLocation _rl;
016    TrainIconAnimation _previous;
017    private static final int bump = 2;
018
019    public TrainIconAnimation(TrainIcon trainIcon, RouteLocation rl, TrainIconAnimation previous) {
020        _trainIcon = trainIcon;
021        _rl = rl;
022        _previous = previous;
023        setName("TrainIconAnimation"); // NOI18N
024    }
025
026    @Override
027    public void run() {
028        // we need to wait for any previous icon animation to complete
029        if (_previous != null) {
030            while (_previous.isAlive()) {
031                sleep();
032            }
033        }
034        log.debug("Icon animation starts for train ({})", _trainIcon.getTrain().getName());
035        int x = _trainIcon.getX();
036        int y = _trainIcon.getY();
037        int newX = _rl.getTrainIconX();
038        int newY = _rl.getTrainIconY();
039
040        while (x < newX) {
041            _trainIcon.setLocation(x, y);
042            x = x + bump;
043            sleep();
044        }
045        while (x > newX) {
046            _trainIcon.setLocation(x, y);
047            x = x - bump;
048            sleep();
049        }
050        while (y < newY) {
051            _trainIcon.setLocation(newX, y);
052            y = y + bump;
053            sleep();
054        }
055        while (y > newY) {
056            _trainIcon.setLocation(newX, y);
057            y = y - bump;
058            sleep();
059        }
060        // log.debug("Route location: "+_rl.getName()+" final icon location X: "+newX+" Y: "+newY);
061        _trainIcon.setLocation(newX, newY);
062    }
063
064    private void sleep() {
065        try {
066            sleep(3);
067        } catch (InterruptedException e) {
068
069        }
070    }
071
072    private final static Logger log = LoggerFactory.getLogger(TrainIconAnimation.class
073            .getName());
074
075}