2015-06-29 46 views
1

我在Java中使用Processing來永久繪製線條圖。這需要一個清除矩形來繪製畫出的線條,爲圖的新部分騰出空間。一切正常,但是當我調用一個方法時,清除功能就像之前一樣停止工作。基本上清理工作是通過在當前線路的前面畫一個矩形來工作的。下面是涉及的兩種主要方法:在Java中處理 - 在調用方法後繪製清除矩形中斷

以下是涉及的兩種主要方法。 drawGraph函數可以正常工作,直到我調用基於縮放重繪圖形的redrawGraph。我認爲中心變量是問題的原因,但我無法弄清楚原因。

public void drawGraph() 
 
    { 
 
     checkZoom(); 
 
     int currentValue = seismograph.getCurrentValue(); 
 
     int lastValue = seismograph.getLastValue(); 
 
     step = step + zoom; 
 
     if(step>offset){ 
 
      if(restartDraw == true) 
 
      { 
 
       drawOnGraphics(step-zoom, lastY2, step, currentValue); 
 
       image(graphGraphics, 0, 0); 
 
       restartDraw = false; 
 
      }else{     
 
      drawOnGraphics(step-zoom, lastValue, step, currentValue); 
 
      image(graphGraphics, 0, 0); 
 
      } 
 
     }             // draw graph (connect last to current point                   // increase step - x axis 
 
     float xClear = step+10;                   // being clearing area in front of current graph 
 
     if (xClear>width - 231) { 
 
      xClear = offset - 10;                  // adjust for far side of the screen 
 
     } 
 
     graphGraphics.beginDraw(); 
 
     if (step>graphSizeX+offset) {                 // draw two clearing rectangles when graph isn't split 
 
//    left = bg.get(0, 0, Math.round(step-graphSizeX), height - 200);             // capture clearing rectangle from the left side of the background image 
 
//    graphGraphics.image(left, 0, 0);                   // print left clearing rectangle 
 
//    if (step+10<width) { 
 
//    right = bg.get(Math.round(step+10), 0, width, height - 200);             // capture clearing rectangle from the right side of the background image 
 
//     // print right clearing rectangle 
 
//    } 
 
     } else {                      // draw one clearing rectangle when graph is split 
 
        center = bg.get(Math.round(xClear), lastY2, offset, height - 200);        // capture clearing rectangle from the center of the background image 
 
        graphGraphics.image(center, xClear - 5, 0);// print center clearing rectangle 
 
     } 
 
     if (step > graphSizeX+offset) {                   // reset set when graph reaches the end 
 
        step = 0+offset; 
 
      } 
 
     graphGraphics.endDraw(); 
 
     image(graphGraphics, 0 , 0); 
 
     System.out.println("step: " + step + " zoom: " + zoom + " currentValue: "+ currentValue + " lastValue: " + lastValue); 
 
    }

private void redrawGraph()              //resizes graph when zooming 
{ 
    checkZoom(); 
    Object[] data = seismograph.theData.toArray(); 
    clearGraph(); 
    step = offset; 
    int y2, y1 = 0; 
    int zoomSize = (int)((width - offset)/zoom); 
    int tempCount = 0; 
    graphGraphics.beginDraw(); 
    graphGraphics.strokeWeight(2);                      // line thickness 
    graphGraphics.stroke(242,100,66); 
    graphGraphics.smooth(); 
    while(tempCount < data.length) 
     { 
      try 
      { 
       y2 = (int)data[tempCount]; 
       step = step + zoom; 
       if(step > offset && y1 > 0 && step < graphSizeX+offset){ 
        graphGraphics.line(step-zoom, y1, step, y2); 
       } 
       y1 = y2; 
       tempCount++; 
       lastY2 = y2; 
      } 
      catch (Exception e) 
      { 
       System.out.println(e.toString()); 
      } 
     } 
    graphGraphics.endDraw(); 
    image(graphGraphics, 0, 0); 
    restartDraw = true; 
    } 

任何幫助和批評是值得歡迎的。感謝您寶貴的時間。

+0

如果您發佈[MCVE](http://stackoverflow.com/help/mcve),您將會有更好的運氣。 –

回答

1

我不確定這種方法是否最好。您可以嘗試簡單的東西爲this

// Learning Processing 
// Daniel Shiffman 
// http://www.learningprocessing.com 

// Example: a graph of random numbers 

float[] vals; 

PGraphics graph; 

void setup() { 
    size(400,200); 
    graph = createGraphics(width,height); 

    // An array of random values 
    vals = new float[width]; 
    for (int i = 0; i < vals.length; i++) { 
    vals[i] = random(height); 
    } 
} 


void draw() { 
    graph.beginDraw(); 
    graph.background(255); 
    // Draw lines connecting all points 
    for (int i = 0; i < vals.length-1; i++) { 
    graph.stroke(0); 
    graph.strokeWeight(2); 
    graph.line(i,vals[i],i+1,vals[i+1]); 
    } 
    graph.endDraw(); 
    image(graph,0,0); 

    // Slide everything down in the array 
    for (int i = 0; i < vals.length-1; i++) { 
    vals[i] = vals[i+1]; 
    } 
    // Add a new random value 
    vals[vals.length-1] = random(height);//use seismograph.getCurrentValue(); here instead 

} 

的主要思想是,以循環數組中的最新數據:

// Learning Processing 
// Daniel Shiffman 
// http://www.learningprocessing.com 

// Example: a graph of random numbers 

float[] vals; 

void setup() { 
    size(400,200); 
    smooth(); 
    // An array of random values 
    vals = new float[width]; 
    for (int i = 0; i < vals.length; i++) { 
    vals[i] = random(height); 
    } 
} 


void draw() { 

    background(255); 
    // Draw lines connecting all points 
    for (int i = 0; i < vals.length-1; i++) { 
    stroke(0); 
    strokeWeight(2); 
    line(i,vals[i],i+1,vals[i+1]); 
    } 

    // Slide everything down in the array 
    for (int i = 0; i < vals.length-1; i++) { 
    vals[i] = vals[i+1]; 
    } 
    // Add a new random value 
    vals[vals.length-1] = random(height);//use seismograph.getCurrentValue(); here instead 

} 

您可以使用PGraphics緩衝作爲你的代碼表明容易做同樣的並簡單地從這個移位數組中繪製值。只要你清除前一幀(background()),該圖應該看起來不錯。

+0

我刮掉了舊代碼,並基於這種方法重寫了它,它使代碼更加乾淨。謝謝。 –

+0

很酷,很高興工作。如果有幫助,您可以投票/標記答案。謝謝 :) –