使用Tween引擎的Java代码无法正常工作

时间:2013-11-03 22:11:17

标签: java swing animation graphics

我想为JLabel添加动画效果,因为我使用的是这个库:http://www.aurelienribon.com/blog/projects/universal-tween-engine/
但 此代码不生成动画。我知道我做错了。请提供正确的方法。我已阅读此页面上的教程https://code.google.com/p/java-universal-tween-engine/wiki/GetStarted。但我仍然无法弄明白......

    public class ParticleAccessor extends javax.swing.JFrame implements TweenAccessor<JLabel>{
        public ParticleAccessor() {
            initComponents();
        }

        private void initComponents() {

        }
        public static void main(String args[]) {
            /
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    ParticleAccessor pa = new ParticleAccessor();
                    pa.setVisible(true);
                    TweenManager m = new TweenManager();
                    Tween.registerAccessor(JLabel.class, pa);
                    Tween.to(particle1, ParticleAccessor.POSITION_XY, 1.0f)
                        .target(100, 200)
                        .start(m);

                    Tween.to(particle2, ParticleAccessor.POSITION_XY, 0.5f)
                        .target(0, 0)
                        .ease(Bounce.OUT)
                        .delay(1.0f)
                        .repeatYoyo(2, 0.5f)
                        .start();
                    m.update(50);
                }
            });
        }
        // Variables declaration - do not modify                     
        private static javax.swing.JLabel particle1;
        private static javax.swing.JLabel particle2;
        // End of variables declaration                   

        public static final int POSITION_X = 1;
        public static final int POSITION_Y = 2;
        public static final int POSITION_XY = 3;

        // TweenAccessor implementation

        @Override
        public int getValues(JLabel target, int tweenType, float[] returnValues) {
            switch (tweenType) {
                case POSITION_X: returnValues[0] = target.getX(); return 1;
                case POSITION_Y: returnValues[0] = target.getY(); return 1;
                case POSITION_XY:
                    returnValues[0] = target.getX();
                    returnValues[1] = target.getY();
                    return 2;
                default: assert false; return -1;
            }
        }

        @Override
        public void setValues(JLabel target, int tweenType, float[] newValues) {
            switch (tweenType) {
                case POSITION_X: target.setAlignmentX(newValues[0]); break;
                case POSITION_Y: target.setAlignmentY(newValues[0]); break;
                case POSITION_XY:
                    target.setAlignmentX(newValues[0]);
                    target.setAlignmentY(newValues[1]);
                    break;
                default: assert false; break;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

将TweenManager移到匿名类之外。将其设为公共静态,以便您可以从任何地方访问它。然后,您需要尽可能多地更新您的经理。要做到这一点,你必须创建一个在循环中运行的另一个线程并不断调用manager.update(timePassed)。尝试这样的事情:

public class MyThread
    extends Thread
{
  private long time = System.currentTimeMillis();

  @Override
  public void run()
  {
      while(true){
        MyGUIProgram.tweenManager.update((System.currentTimeMillis() - time)/1000);
        time = System.currentTimeMillis();
      }
  }
}

然后,在主对象的构造函数中启动该线程。