如何为正在处理的饼图创建单独的 class 选项卡?

How to create a seperate class tab for a pie chart in processing?

我想在我的项目中添加一个饼图,但我希望能够有一个 class 选项卡仅用于饼图的编码,并且在 setup() draw() 选项卡中只有输出饼图的代码。我希望能够使 setup() draw() 编码 window 看起来更清晰,因为它在我的项目中被评估过。 下面我提供了我的原始代码,然后我尝试为饼图的代码制作一个单独的 class 选项卡。

我得到的错误是“构造函数“PieChart(int, int, int, int[], int[])”在 setup() draw() 选项卡的第 8 行中不存在”并且 'Return type for the method is missing' 在 PieChart 选项卡的第 7 行。

原代码:

int[] values = {32, 11, 7};
int[] colors = {#E4000F, #655CBE, #107C10};

void setup() {
  size(300, 300);
  pixelDensity(2); 
  smooth();
}

void draw() {
  background(25);
  pieChart(width/2, height/2, 200, values, colors);
}

// pX, pY : position
// pRad : diameter
// pVal: Array of Values
// pCols: Array of colors

void pieChart(float pX, float pY, float pDia, int[] pVal, int[]pCols) {
  stroke(225);
  float total = 0;
  float lastAngle= -PI;
  float mouseAngle = atan2(mouseY-pY, mouseX-pX);

  // get sum of values
  for (int i =0; i<pVal.length; i++) {
    total += pVal[i];
  }

  for (int i =0; i<pVal.length; i++) {
    fill(pCols[i]);
    float angle = map(pVal[i], 0, total, 0, 2*PI);
    arc(pX, pY, pDia, pDia, lastAngle, lastAngle+angle, PIE);

    if ( mouseAngle >= lastAngle && mouseAngle < lastAngle+angle ) {
      text(values[i] + "/50", pX-pDia/2, pY-pDia/2);
    }

    lastAngle += angle;
  }
}

尝试制作单独的 class 代码(setup() draw() 选项卡):

int[] values = {32, 11, 7};
int[] colors = {#E4000F, #655CBE, #107C10};

PieChart pieChart;

void setup() {
  size(300, 300);
  pieChart = new PieChart(width/2, height/2, 200, values, colors);
}

void draw() {
  background(25);
}

尝试制作单独的 class 代码(饼图选项卡):

class PieChart {
  //pX, pY : position
  // pRad : diameter
  // pVal: Array of Values
  // pCols: Array of colors

  pieChart(float pX, float pY, float pDia, int[] pVal, int[]pCols) {
    noStroke();
    float total = 0;
    float lastAngle= -PI;
    float mouseAngle = atan2(mouseY-pY, mouseX-pX);


    // get sum of values
    for (int i =0; i<pVal.length; i++) {
      total += pVal[i];
    }

    for (int i =0; i<pVal.length; i++) {
      fill(pCols[i]);
      float angle = map(pVal[i], 0, total, 0, 2*PI);
      arc(pX, pY, pDia, pDia, lastAngle, lastAngle+angle, PIE);

      if ( mouseAngle >= lastAngle && mouseAngle < lastAngle+angle ) {
        text(values[i] + "/50)", pX-pDia/2, pY-pDia/2);
      }
      lastAngle += angle;
    }
  }
}

非常感谢任何帮助,非常感谢。

以下在我的系统 Java 中运行:

int[] values = {32, 11, 7};
int[] colors = {#E4000F, #655CBE, #107C10};

class PieChart {

void pieChart(float pX, float pY, float pDia, int[] pVal, int[]pCols) {
    noStroke();
    float total = 0;
    float lastAngle= -PI;
    float mouseAngle = atan2(mouseY-pY, mouseX-pX);

    // get sum of values
    for (int i =0; i<pVal.length; i++) {
      total += pVal[i];
    }

    for (int i =0; i<pVal.length; i++) {
      fill(pCols[i]);
      float angle = map(pVal[i], 0, total, 0, 2*PI);
      arc(pX, pY, pDia, pDia, lastAngle, lastAngle+angle, PIE);

      if ( mouseAngle >= lastAngle && mouseAngle < lastAngle+angle ) {
        text(values[i] + "/50)", pX-pDia/2, pY-pDia/2);
      }
      lastAngle += angle;
    }
  }
}

PieChart pieChart;

void setup() {
  size(300, 300);
  pixelDensity(2); 
  smooth();
  pieChart = new PieChart();
}

void draw() {
  background(25);
  pieChart.pieChart(width/2.0, height/2.0, 200.0, values, colors);
}