2017-09-23 4 views
-2

現在、2つの株式交換アルゴリズムを比較するアニメーションを作成中です。 JComponentを拡張しているペイントコンポーネント内でアルゴリズムを実行しています。 (最高ではありませんが、私は気にしません)画面の半分をペイントコンポーネントの途中でリフレッシュする必要があります。私はそれが画面の日付を設定する前にすべての方法を取得する必要はありません。その理由は、私はネストされたwhileループを持つアルゴリズムと、入れ子になっていないwhileループを持つアルゴリズムを持っているからです。これをどうやってやりますか?オブジェクトをReapintの途中でレンダーする

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

どのような意味があるのか​​分かりません。

コンポーネントの半分を意味する場合、単純な方法は2つのJComponentを使用することです。

同じ構成要素にある場合、1行は更新されますが、別の行は更新されません。

私がrepsを理解するのは、pack()、updateUI()、またはinvalidate()によって引き起こされた呼び出しです。 私の見解では、repaint()はこれらの行/ 2Dを塗りつぶすだけで、これらのループを実行する/別のスレッドでこれらのデータを生成する必要があります。データ収集が完了したら、updateUI()/ paintImmediatelyを呼び出してください。

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

私はpaintコンポーネントでrepaintメソッドを呼び出すと、再帰関数にならないでしょうか? –

+0

どのように 'repaint'を呼び出すと何が解決されますか? @MorrisSowards:ペイントメソッドの中から 'repaint()'を呼ぶべきではないが、ペインティングマネージャーはこれを再帰的にするつもりはない。 –

+0

@MorrisSowards:しかし、ポイントまで、なぜ、あなたはforqzyの答えにコメントしていますか、私の答えではなく、実際には実用的な答えではありません - SwingWorkerを使うには? –

0

私はJComponentのを拡張する塗料コンポーネント内のアルゴリズムを実行しています。 (ない最高のが、私は気にしない)

しかし、あなたべきケア、あなたの問題と可能な解決策に影響を及ぼし以来。

ペイントコンポーネントの途中で画面を更新する必要があります。私はそれが画面の日付を設定する前にすべての方法を取得する必要はありません。その理由は、私はネストされたwhileループを持つアルゴリズムと、入れ子になっていないwhileループを持つアルゴリズムを持っているからです。これをどうやってやりますか?

次に、paintComponentによってアルゴリズムを実行しないでください。代わりにSwingWorker<Void, Image>を使用し、BufferedImageを更新し、その画像をワーカーの公開/処理方法の組でGUIに渡します(repaint())。 SwingWorkerの使い方の詳細については、以下をご覧ください。Lesson: Concurrency in Swing

関連する問題