⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animator.java

📁 无线传感器网络节点Sun SPOT管理工具
💻 JAVA
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.  *  * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. *  * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. *  * Use is subject to license terms.  *  * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.  *  * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. *  * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. *  * L'utilisation est soumise aux termes du contrat de licence. *  * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.gridview;/** * For use in GridView animations * @author randy */public class Animator extends Thread {    //Basic variables set in initialization calls ( calls to init(...) )    protected GVObject target;  //This is the animatee, the thing being moved.    protected GVObject goal;    // If not null, this is the object to which the target moves    int xFinal, yFinal;               // This is the final location    int xOffset, yOffset;             // If goal == null, these are null, else they give the final position releative to the goal object.    double duration, dt, tIn, tOut;        //Following are more practical variables derived from the above ...    double locX, locY, dX, dY, t, tMid, vMaxX, vMaxY, vX, vY, aX, aY;        protected Animator nextAnimator;    boolean shouldRun;      //Boolean checked at each step in animation.        public Animator(GVTangibleObject targ, int xF,  int yF) {        this(targ, null,xF, yF, 1.0, 0.05, 0.1, 0.1);    }        public Animator(GVObject targ,  GVObject goalObject, int xF,  int yF,  double dur,  double deltaT,  double slowIn,  double slowOut ) {        target = targ;        goal = goalObject;        if(goal == null){            //xOffest and yOffset are left null in this case.            xFinal = xF;            yFinal = yF;        } else {            xOffset = xF;            yOffset = yF;            xFinal = goal.getLocationInView().x + xOffset;            yFinal = goal.getLocationInView().y + yOffset;        }        duration = dur;        dt = deltaT;        tIn  = dur * slowIn;        tOut = dur * slowOut;        tMid = duration - tIn - tOut;    }        public void run(){        runNew();    }        public void runOld(){        locX = (target.getLocationInView()).x;        locY = (target.getLocationInView()).y;        dX = xFinal - locX;        dY = yFinal - locY;        vMaxX = dX/(duration - 0.5*(tIn + tOut));        vMaxY = dY/(duration - 0.5*(tIn + tOut));        vX = 0.0;        vY = 0.0;        t = 0.0;        shouldRun = true;        while(shouldRun){            if(t<duration){                if(t<tIn){                    aX = vMaxX/tIn;                    aY = vMaxY/tIn;                } else if (t < duration - tOut){                    aX = 0.0;                    aY = 0.0;                } else {                    aX = - vMaxX/tOut;                    aY = - vMaxY/tOut;                }                vX = vX + aX*dt;                vY = vY + aY*dt;                locX = locX + dt*vX + 0.5*dt*dt*aX;                locY = locY + dt*vY + 0.5*dt*dt*aY;                target.setLocationInView((int)locX, (int)locY);                target.repaint();                try {                    Thread.sleep((int)(dt*1000));                } catch (InterruptedException e) {                    e.printStackTrace();                }                t = t + dt;            } else {                target.setLocationInView(xFinal, yFinal);                target.repaint();                shouldRun = false;                startNextAnimator();            }            yield();        }    }        public void runNew(){        locX = (target.getLocationInView()).x;        locY = (target.getLocationInView()).y;        t = 0.0;        vX = 0.0;        vY = 0.0;        shouldRun = true;                while(shouldRun){            if(t < duration){                setXYFinal();                if(t < tIn){                    doKinematics1();                } else if(t < duration - tOut){                    doKinematics2();                } else {                    doKinematics3();                }                target.setPositionAndMoveLoosePieces((int) locX, (int) locY);                target.repaint();                                try {                    Thread.sleep((int)(dt*1000));                } catch (InterruptedException e) {                    e.printStackTrace();                }                t = t + dt;                yield();            } else {                target.setPositionAndMoveLoosePieces(xFinal, yFinal);                target.repaint();                shouldRun = false;                andThenDo();                startNextAnimator();            }        }    }        public void setXYFinal() {        if(goal != null){            xFinal = goal.getLocationInView().x + xOffset;            yFinal = goal.getLocationInView().y + yOffset;            if (xFinal < -200) {                int i = 2;            }         }    }        /*     * Placeholder for any actions youmay want performed after this animator completes, but before     * the next one starts.     **/    public void andThenDo(){            }    /*     * Compute the new position locX, locY, and the new velocity vX and vY, in the slow in portion of the motion.     */    private void doKinematics1(){        double vFx, vFy, a1x, a1y;                double tDenom = tIn/2.0 + tMid + tOut/2.0 - t/2.0;                vFx = (xFinal - locX - vX*((tIn - t) / 2.0))/ tDenom;        vFy = (yFinal - locY - vY*((tIn - t) / 2.0))/ tDenom;                a1x = (vFx - vX) / (tIn - t);        a1y = (vFy - vY) / (tIn - t);                locX = locX + vX*dt + 0.5 * a1x*dt*dt;        locY = locY + vY*dt + 0.5 * a1y*dt*dt;                vX = vX + a1x * dt;        vY = vY + a1y * dt;    }        /*     * Compute the new position locX, locY in the middle, constant speed portion of the motion.     */    private void doKinematics2(){        vX = (xFinal - locX)/(tIn + tMid - t + 0.5 * tOut);        vY = (yFinal - locY)/(tIn + tMid - t + 0.5 * tOut);        locX = locX + vX*dt;        locY = locY + vY*dt;    }        /*     * Compute the new position locX, locY in the slow out portion of the motion.     */    private void doKinematics3(){        double a3x, a3y;  /// ... the NEAGTIVE of the literal acceleration.        vX = 2.0 * (xFinal - locX)/(duration - t);        vY = 2.0 * (yFinal - locY)/(duration - t);        a3x = vX / (duration - t);        a3y = vY / (duration - t);                locX = locX + vX*dt - 0.5 * a3x*dt*dt;        locY = locY + vY*dt - 0.5 * a3y*dt*dt;    }                public GVObject getTarget() {        return target;    }        public void setTarget(GVObject target) {        this.target = target;    }        /*     *Just starts the next Animator approproiately., This Animator continues running on its own.     ***/    public void startNextAnimator(){        if(nextAnimator != null){            target.setCurrentAnimator(nextAnimator);            nextAnimator.setTarget(target);            nextAnimator.run();        } else {            target.setCurrentAnimator(null);        }    }        /*     *Public way to stop this thread at the next step and start the next Animator (if it exsists)     ***/    public void runNextAnimation(){        shouldRun = false;    }        /*     *Public way to stop entire animation chain.     */    public synchronized void stopAllAnimations(){        nextAnimator = null;        shouldRun = false;    }        /*     * Add the argument newA at the end of the animator chain.     *     */    public void addAnimator(Animator newA){        Animator a = this;        while(a.getNextAnimator() != null){            a = a.getNextAnimator();        }        a.setNextAnimator(newA);    }        public Animator getNextAnimator() {        return nextAnimator;    }        public void setNextAnimator(Animator nextAnimator) {        this.nextAnimator = nextAnimator;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -