java - Java 中的处理 - 调用方法后绘制的清除矩形会中断

标签 java drawing processing

我正在使用 Java 中的处理来永久绘制折线图。这需要一个清除矩形来绘制绘制的线条,以便为图形的新部分腾出空间。一切工作正常,但是当我调用一个方法时,清理工作就像以前一样停止工作。基本上,清除的工作原理是在当前线所在的位置绘制一个矩形

下面是涉及的两个主要方法。在我调用 redrawGraph 之前,drawGraph 函数工作正常,它根据缩放重新绘制图形。我认为中心变量是问题的原因,但我不明白为什么。

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;
    } 

欢迎任何帮助和批评。感谢您抽出宝贵的时间。

最佳答案

我不确定这种方法是否是最好的。您可以尝试像this这样简单的事情:

// 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 缓冲区轻松执行相同的操作:

// 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

}

主要思想是循环数组中的最新数据,并简单地从这个移位数组中提取值。只要清除前一帧 (background()),图表看起来就应该没问题。

关于java - Java 中的处理 - 调用方法后绘制的清除矩形会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31118895/

相关文章:

java - 在 Swing GUI Java 面板中绘制多边形

user-interface - 在加工草图中,我可以控制显示窗口的位置吗?

Java Android 在 FrameLayout 中创建 View

java - 在 recyclerview 中添加部分

android - 存储绘图 Canvas 并稍后恢复

javascript - 我怎样才能在 1 个 Canvas 上同时使用 javascript 制作多个动画

opengl - 如何在处理中更改 GLSL 着色器参数

java - 带有处理功能的 Skype API 或 Arduino

java - 无法推断 ArrayList<> 的类型参数

java - 无法在 Ubuntu 上启动 Hive