2015-11-08 53 views
1

我目前正在使用JavaFX中的動畫處理應用程序。正在糾正計算機生成的字幕的人類矯正師使用該應用程序。在動畫中有一個浮動文字。我的問題是動畫有時會關閉。你可以看到下面的圖片演示:主要是調整後出現JavaFX動畫問題

enter image description here

這個漏洞。當動畫中斷時,它不會再次進入完全運行狀態。 我使用插在Swing UI中的JFXpanel。我以這種方式使用它,因爲我在Swing中創建了很多代碼,我不想把它全部扔掉。我不使用Swing進行動畫製作,因爲我無法創建足夠流暢的動畫。 這裏是動畫相關的代碼:

public class AnimationPanel extends JFXPanel { 

    public MyAnimationTimer animationTimer; 
    public EditObject editObject; 

    public Color colorHOST1; 
    public Color colorHOST2; 
    public Color colorGUEST1; 
    public Color colorGUEST2; 
    public Color colorUSER; 
    public Color colorSIGNING; 

    public Color basicColor = Color.WHITE; 

    public Color currentColor = Color.WHITE; 

    public AnimationPanel(EditObject editObject) { 
     super(); 

     this.editObject = editObject; 

     Group group = new Group(); 

     this.animationTimer = new MyAnimationTimer((List<MyText>)(List<?>)group.getChildren(), this); 

     final Scene scene = new Scene(group, 800, 600, Color.BLACK); 

     this.setScene(scene); 

     this.animationTimer.start(); 

/*  // Update animation when component is resized 
     this.addComponentListener(new ComponentListener() { 
      @Override 
      public void componentResized(ComponentEvent e) { 
       animationTimer.updateAnimations(); 
      } 

      @Override 
      public void componentMoved(ComponentEvent e) { 
      } 

      @Override 
      public void componentShown(ComponentEvent e) { 
      } 

      @Override 
      public void componentHidden(ComponentEvent e) { 
      } 
     });*/ 

    } 

    public void setColors(Gui g) { 
     this.colorHOST1 = Color.rgb(g.colorHOST1.getRed(), g.colorHOST1.getGreen(), g.colorHOST1.getBlue(), g.colorHOST1.getAlpha()/255.0); 
     this.colorHOST2 = Color.rgb(g.colorHOST2.getRed(), g.colorHOST2.getGreen(), g.colorHOST2.getBlue(), g.colorHOST2.getAlpha()/255.0); 
     this.colorGUEST1 = Color.rgb(g.colorGUEST1.getRed(), g.colorGUEST1.getGreen(), g.colorGUEST1.getBlue(), g.colorGUEST1.getAlpha()/255.0); 
     this.colorGUEST2 = Color.rgb(g.colorGUEST2.getRed(), g.colorGUEST2.getGreen(), g.colorGUEST2.getBlue(), g.colorGUEST2.getAlpha()/255.0); 
     this.colorUSER = Color.rgb(g.colorUSER.getRed(), g.colorUSER.getGreen(), g.colorUSER.getBlue(), g.colorUSER.getAlpha()/255.0); 
     this.colorSIGNING = Color.rgb(g.colorSIGNING.getRed(), g.colorSIGNING.getGreen(), g.colorSIGNING.getBlue(), g.colorSIGNING.getAlpha()/255.0); 

    } 
} 



public class MyAnimationTimer extends AnimationTimer { 
    private List<MyText> nodes; 
    private long subtitle_max_time_in_app; 
    private AnimationPanel animationPanel; 
    private boolean stopAtTheEnd = false; 
    private boolean isAtTheEnd = false; 
    private int currentPos = 0; 

    public MyAnimationTimer(List<MyText> nodes, AnimationPanel animationPanel) { 
     super(); 
     this.nodes = nodes; 
     this.animationPanel = animationPanel; 
    } 

    @Override 
    public void handle(long now) { 
     MyText node; 
     if(this.stopAtTheEnd) { 
      if(this.isAtTheEnd) { 
       for (int i = this.currentPos; i < this.nodes.size(); i += 2) { 
        node = nodes.get(i); 
        if(this.collides(nodes.get(i-2), node)) { 
         node.setTranslateXforTextandSubText(nodes.get(i-2).getBoundsInParent().getWidth() + nodes.get(i-2).getTranslateX() + 10); 
         this.currentPos+=2; 
        } 
        node.setTranslateXforTextandSubText(node.getTranslateX() - node.getVelocity()); 
       } 
      } else { 
       if(nodes.size()!=0) { 
        node = nodes.get(0); 
        if((node.getTranslateX() - node.getVelocity()) < 0) { 
         node.setTranslateXforTextandSubText(0); 
         this.isAtTheEnd = true; 
         this.currentPos = 2; 
        } else { 
         for (int i = 0; i < this.nodes.size(); i += 2) { 
          node = nodes.get(i); 
          node.setTranslateXforTextandSubText(node.getTranslateX() - node.getVelocity()); 
         } 
        } 
       } 
      } 
     } else { 
      for (int i = 0; i < this.nodes.size(); i += 2) { 
       node = nodes.get(i); 
       node.setTranslateXforTextandSubText(node.getTranslateX() - node.getVelocity()); 
      } 
     } 
    } 

    private boolean collides(MyText node1, MyText node2) { 
     return (node1.getBoundsInParent().getWidth() + node1.getTranslateX() - node2.getTranslateX()) + 7 >= 0; 
    } 

    public void addNode(final MyText node) { 
     Platform.runLater(() -> { 
      node.setTranslateYforTextandSubText(animationPanel.getHeight()/2); 

      node.setTranslateXforTextandSubText(animationPanel.getWidth()); 

      node.setVelocity(this.getVelocity()); 
      nodes.add(node); 
      nodes.add(node.id); 

      // Check for overlaying 
      if(nodes.size()>=4) { 
       int size = nodes.size(); 
       double overlaying = (nodes.get(size-4).getBoundsInParent().getWidth() + nodes.get(size-4).getTranslateX() - nodes.get(size-2).getTranslateX()) + 7; 
       if(overlaying>0) { 
        nodes.get(size-2).setTranslateXforTextandSubText(nodes.get(size-2).getTranslateX()+overlaying); 
       } 
      } 
     }); 
    } 

    public void recalculateGaps() { 
     Platform.runLater(() -> { 
      if (nodes.size() >= 4) { 
       double overlaying; 
//    System.out.println("Size: " + nodes.size()); 
       for (int i = nodes.size() - 2; i > 0; i -= 2) { 
        overlaying = (nodes.get(i - 2).getBoundsInParent().getWidth() + nodes.get(i - 2).getTranslateX() - nodes.get(i).getTranslateX()) + 7; 
        if (overlaying > 0) { 
         nodes.get(i - 2).setTranslateXforTextandSubText(nodes.get(i - 2).getTranslateX() - overlaying); 
        } 
       } 
      } 
     }); 
    } 

    public void removeNodesBehindTheScene() { 
     Platform.runLater(() -> { 
      MyText node; 
      for (int i=0; i<nodes.size(); i+=2) { 
       node = nodes.get(i); 
       if(node.getTranslateX() > 0) { 
        break; 
       } else { 
        if(!node.isOverdue()) { 
         animationPanel.editObject.setMessageToBeSendSoon(node); 
        } 
        nodes.remove(i); 
        nodes.remove(i); 
        i-=2; 
       } 
      } 
     }); 
    } 

/* public void updateAnimations() { 
//  This method is called when the window is resized. 
     for (int i=0; i<this.nodes.size(); i+=2) { 
      nodes.get(i).setTranslateYforTextandSubText(animationPanel.getHeight()/2); 
     } 
     this.setVelocity(); 
    }*/ 

    public double getVelocity() { 
     return (this.animationPanel.getWidth()/4)*3/((double) this.subtitle_max_time_in_app)*1000/60; 
    } 

    public void setSubtitle_max_time_in_app(long subtitle_max_time_in_app) { 
     this.subtitle_max_time_in_app = subtitle_max_time_in_app; 
    } 

    public void setStopAtTheEnd(boolean stopAtTheEnd) { 
     // Remove all overdue 
     if(stopAtTheEnd) { 
      Platform.runLater(() -> { 
       for (int i = 0; i < nodes.size(); i += 2) { 
        if (nodes.get(i).isOverdue()) { 
         nodes.remove(i); 
         // Remove ID number 
         nodes.remove(i); 
         i -= 2; 
        } else { 
         break; 
        } 
       } 
      }); 
      this.isAtTheEnd = false; 
      this.currentPos = 0; 
     } 
     this.stopAtTheEnd = stopAtTheEnd; 
    } 

    public void removeUpToNode(MyText node) { 
     Platform.runLater(() -> { 
      if(nodes.contains(node)) { 
       for (int i = 0; i < nodes.size(); i += 2) { 
        if (nodes.get(i) == node) { 
         nodes.remove(i); 
         nodes.remove(i); 
         break; 
        } 
        else { 
         nodes.remove(i); 
         nodes.remove(i); 
         i-=2; 
        } 
       } 
      } 
     }); 
    } 

    public void addNodesAtTheBeginning(List<MyText> nodes_list, double nodeposition) { 
     Platform.runLater(() -> { 
      MyText node; 
      double position; 
      for (int i = nodes_list.size() - 1; i >= 0; i--) { 
       node = nodes_list.get(i); 

       node.setTranslateYforTextandSubText(animationPanel.getHeight()/2); 

       if(nodes.size()!=0) { 
        position = this.nodes.get(0).getTranslateX() - node.getBoundsInParent().getWidth() - 10; 
       } else { 
        position = animationPanel.getWidth(); 
       } 

       if(i==(nodes_list.size() - 1)) { 
        double exactposition = nodeposition - node.getBoundsInParent().getWidth(); 
        if(exactposition < position) { 
         node.setTranslateXforTextandSubText(exactposition); 
        } else { 
         node.setTranslateXforTextandSubText(position); 
        } 
       } else { 
        node.setTranslateXforTextandSubText(position); 
       } 

       node.setVelocity(this.getVelocity()); 
       nodes.add(0, node.id); 
       nodes.add(0, node); 
      } 
     }); 
    } 
} 

我測試過的JavaFX的各種版本(包括一個裝在JDK9),但沒有結果。在此先感謝

+0

您的代碼不可編譯。 – Roland

+0

這只是我的應用程序的片段,它與動畫相關,並且沒有其他部分就不能編譯。我沒有把我的整個應用程序放在這裏,因爲沒有我不會在這裏發佈的服務器的訪問權限是沒用的。我創建了這個帖子,看看是否有人與JFXpanel有類似的問題。 –

+0

然後你應該創建一個[MCVE](http://stackoverflow.com/help/mcve)。 – Roland

回答

2

最後我修復了這個錯誤。問題是我從我自己的線程而不是JavaFX線程設置了現有節點的屬性。把它放在Platform.runLater方法中修復它。我沒有立即注意到這個錯誤,因爲它沒有拋出非法線程異常,就像它在你嘗試添加節點時一樣。我應該更徹底地使文件變紅。 謝謝