2013-11-03 38 views
0

我想爲JLabel添加動畫效果,因爲我使用此庫:http://www.aurelienribon.com/blog/projects/universal-tween-engine/
但是 此代碼不生成動畫。我知道我做錯了。請提供正確的方法。我已閱讀此頁上的教程​​。但我仍然無法弄清楚.....使用吐溫引擎不工作的Java代碼

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; 
      } 
     } 
    } 

回答

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(); 
     } 
    } 
} 

然後,在主對象的構造函數中啓動該線程。