2017-09-23 4 views
-2

현재 두 개의 증권 거래소 알고리즘을 비교하기 위해 애니메이션 작업 중입니다. JComponent를 확장하는 페인트 구성 요소 내에서 알고리즘을 실행 중입니다. (최고는 아니지만 상관하지 않습니다.) 페인트 구성 요소를 통해 화면을 절반 새로 고침해야합니다. 화면에 날짜가 표시되기 전에 모든 것이 끝나기를 원하지 않습니다. 그 이유는 중첩 된 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

중간 의미를 이해하지 못합니다.

구성 요소의 절반을 의미하는 경우 간단한 방법은 두 개의 JComponent를 사용하는 것입니다.

동일한 구성 요소에있는 경우 한 줄은 업데이트되었지만 다른 줄은 업데이트되지 않은 것을 의미합니다.

repaint이 packs(), updateUI() 또는 invalidate()에 의해 호출되었을 때의 이해. 제 견해로, repaint()는이 라인들/2D를 그리는 것에 관심을 가져야 만하고, 다른 스레드는이 루프들을 실행하거나이 데이터들을 생성합니다. 데이터 수집이 끝나면 updateUI()/paintImmediately를 호출하십시오. 당신은 도장 시스템은 [여기]를 작동하는 방법에 대해 읽어야

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

페인트 구성 요소에서 repaint 메서드를 호출하면 재귀 함수가되지 않습니까? –

+0

어떻게'repaint'를 호출하면 문제가 해결 될까요? @MorrisSowards : 당신은 paint 메소드 내에서'repaint()'를 호출하면 안되지만, painting manager는 이것을 재귀 적으로 만들지 않을 것입니다. –

+0

@MorrisSowards :하지만 더 중요한 것은 왜 forqzy의 답변, 기초가 아닌 대답, 내 대답이 아니라 실제로 실용적인 대답인가? SwingWorker를 사용하는 것입니다. –

0

나는 JComponent의 연장 페인트 구성 요소 내에서 알고리즘을 실행하고 있습니다. (안 최고,하지만 난 상관 없어)

하지만 해야 관리, 문제점 및 해결 방안에 대한이 영향입니다.

페인트 구성 요소를 통해 화면을 절반 새로 고침해야합니다. 화면에 날짜가 표시되기 전에 모든 것이 끝나기를 원하지 않습니다. 그 이유는 중첩 된 while 루프가있는 하나의 알고리즘과없는 다른 알고리즘이 있다는 것입니다. 이 일을 어떻게 하죠?

그런 다음 알고리즘을 paintComponent를 통해 실행하지 마십시오. 대신 SwingWorker<Void, Image>을 사용하고, BufferedImage를 업데이트하고 작업자의 게시/처리 메소드 쌍인 repaint()을 동시에 호출하여 해당 이미지를 GUI로 전달하십시오. SwingWorker를 사용하는 방법에 대한 자세한 내용은 다음을 참조하십시오. Lesson: Concurrency in Swing

관련 문제