2017-09-23 83 views
-2

我目前正在研究一個動畫來比較兩種股票交換算法。我在擴展JComponent的paint組件中運行算法。 (不是最好的,但我不在乎)我需要通過繪畫組件中途刷新屏幕。我不想讓它在日期顯示之前一路通過。原因是我有一個嵌套while循環的算法,另一個沒有。我會如何去做這件事?通過重塑使對象呈現一半

public void paintComponent(Graphics g) {    
    //calls in the super class and calls the calibrate the graphics method 
    super.paintComponent(g); 
    Graphics2D g2D = (Graphics2D) g; 
    calibrateFrame(getHeight(), getWidth()); 

    //Clears the rectangle to avoid overlaying, makes it black 
    g2D.clearRect(0, 0, getWidth(), getHeight()); 
    g2D.setColor(Color.BLACK); 
    g2D.fillRect(0, 0, getWidth(), getHeight()); 

    //Draws the rectangles without the algorithm started 
    redraw(g2D, -1); 


    /** 
    *algorithms 
    */ 
    fastSpans[0] = 1; 
    slowSpans[0] = 1; 

    //Makes a new stack pushes 0 on the stack 
    Stack myStack = new Stack(); 
    myStack.push(0); 

    //If it has not been sorted or paused, continue the algorithm 
    if (!(pause) && !(sorted)){ 

     //The slower algorithm needs to start out at zero (j) 
     int j = indexValue-1; 

     g2D.setColor(Color.BLUE); 

     //Calculates the values for the X and Y coordinates for the 
     //new rectangle, along with the height 
     int slowY = calSlowY(j); 
     int slowX = calSlowX(j); 
     int curHeightSlow = (int) ((stocks[j]/maxStockValue)*maxHeight); 

     //Here is the actual algorithm 
     int k = 1; 
     boolean span_end = false; 
     //Nested While Loop 
     while (((j-k)>0) && !span_end){ 
      if (stocks[j-k] <= stocks[j]){ 
       k = k + 1; 
       // Draw the current component 
       // ********************** 
       // DO REFRESH MID PAINT COMPONENT 
      } 
      else{ span_end = true; } 
     } 
     slowSpans[j] = k; 
     g2D.setColor(Color.WHITE); 
     for(int i = 0; i < numberOfStock ; i++){ 


     } 

     if (!(indexValue >= numberOfStock)){ 
      while (!(myStack.empty()) && (stocks[(int)myStack.peek()]) <= stocks[indexValue]){ 
       myStack.pop(); 
      } 
      if (myStack.empty()){ 
       fastSpans[indexValue] = indexValue + 1; 

      } 
      else { 
       fastSpans[indexValue]= indexValue - (int) myStack.peek(); 
       //System.out.println("Im in the else"); 
      } 
      myStack.push(indexValue); 
     } 
    } 
    drawStrings(g2D); 
} 
+2

你應該閱讀有關的繪畫系統是如何工作的[這裏](http://www.oracle.com/technetwork/java/painting-140037.html),然後用任何其他問題回來。對於所有的意圖和目的,你應該考慮paintComponent是一個原子操作。 –

回答

-2

不明白中途是什麼意思。

如果它意味着組件的一半,那麼簡單的方法就是使用兩個JComponent。

如果u意味着相同的組件中有一行已更新,但另一行未更新。

我的理解是重新打包時調用packs(),updateUI()或由invalidate()引起的。 所以在我看來,repaint()應該只關心繪製這些線/ 2D,並在另一個線程中執行這些循環/生成這些數據。數據收集完成後,立即調用updateUI()/ paintIm。

SwingUtilities.invokeLater(new Runnable() 
{ 
    public void run() 
    { 
     repaint(); 
    } 
}); 
+0

如果我在paint組件中調用repaint方法,那不會讓它成爲遞歸函數嗎? –

+0

調用'repaint'如何解決任何問題? @MorrisSowards:你說得對,你不應該在繪畫方法中調用'repaint()',但繪畫管理器不會讓它變成遞歸的。 –

+0

@MorrisSowards:但更重要的是,爲什麼你要評論forqzy的答案,一個是基礎的,而不是我的答案,這是一個實際的答案 - 使用SwingWorker? –

0

我正在擴展JComponent的油漆組件內的算法。 (不是最好的,但我不在乎)

但你應該照顧,因爲你的問題以及可能的解決這一影響。

我需要屏幕刷新通過油漆組件的一半。我不想讓它在日期顯示之前一路通過。原因是我有一個嵌套while循環的算法,另一個沒有。我會如何去做這件事?

然後不要通過paintComponent運行算法。請使用SwingWorker<Void, Image>,更新BufferedImage並通過工作人員的發佈/處理方法對將該圖像傳遞給GUI,同時調用repaint()。有關如何使用SwingWorker的更多信息,請參閱:Lesson: Concurrency in Swing