(Java/处理)如何在屏幕上的不同位置创建一个对象的多个实例?

(Java/处理)如何在屏幕上的不同位置创建一个对象的多个实例?,java,processing,Java,Processing,我无法创建waveClock对象的多个实例,即使我已将其放入数组并标记了每个对象的中心位置。我想在一个窗口中创建4个对象,所有对象都响应不同的声音频率/节拍等 有人能告诉我怎么做吗?我认为这可能是waveClock类中的centerX和centerY变量的问题 ArrayList<waveClock> waveClocks = new ArrayList<waveClock>(); //global variables float angnoise, radiusnoi

我无法创建
waveClock
对象的多个实例,即使我已将其放入数组并标记了每个对象的中心位置。我想在一个窗口中创建4个对象,所有对象都响应不同的声音频率/节拍等

有人能告诉我怎么做吗?我认为这可能是
waveClock
类中的
centerX
centerY
变量的问题

ArrayList<waveClock> waveClocks = new ArrayList<waveClock>();

//global variables
float angnoise, radiusnoise;
float xnoise, ynoise;
float angle = -PI/6;
float radius;
float strokeCol = 254;
int strokeChange = -1;
int speed; //changes speed of visualisation once beat is detected?

void setup() 

  //for every waveClock we need 180 pixels width, then add 20 pixels for first gap
  size(740, 650);
  background(255);

  //code is called
  waveClocks.add(new waveClock(100, height/2, minRadius, bassColour, lowBassBand, highBassBand, numberOfLowOnsetsThreshold));
  waveClocks.add(new waveClock(280, height/2, minRadius, midColour, lowMidBand, highMidBand, numberOfMidOnsetsThreshold));  
  waveClocks.add(new waveClock(460, height/2, minRadius, highColour, lowHighBand, highHighBand, numberOfHighOnsetsThreshold));
  waveClocks.add(new waveClock(640, height/2, minRadius, veryHighColour, lowVeryHighBand, highVeryHighBand, numberOfVeryHighOnsetsThreshold));

  //set the min and max radius of each of the viz circles
 /* for (int i = 0; i < waveClocks.size(); i++) {
    //go through the arraylist of waveClocks and set the min and max radius of each circle
    waveClocks.get(i).setMinMaxRadius(minRadius, maxRadius);
  }*/

  song.play();

  beat = new BeatDetect(song.bufferSize(), song.sampleRate());

  bl = new BeatListener(beat, song);
}

void draw() {
  //clear the screen by painting it black
  //background(0);

  for (int i = 0; i < waveClocks.size(); i++) {

    //has there been a beat in the range? get(circle ID).low band, high band etc.
    if (beat.isRange(waveClocks.get(i).getLowBand(), waveClocks.get(i).getHighBand(), waveClocks.get(i).getOnsetThreshold())) {
      waveClocks.get(i).setMaxRadius();
    }
    //waveClocks.get(i).drawCircle();
    waveClocks.get(i).drawWaveClock();
  }
}
}

void drawWaveClock(){
半径噪声+=0.005;
半径=(噪声(半径噪声)*350)+1;
噪声+=0.005;
角度+=(噪声(angnoise)*6)-3;
如果(角度>360){
角度-=360;
}否则,如果(角度<0){
角度+=360;
}
xnoise+=0.01;
ynoise=+0.01;
浮动中心X=宽度/2+(噪声(X噪声)*100)-50;
浮动中心Y=高度/2+(噪声(Y噪声)*100)-50;
浮动半径=弧度(角度);
浮动x1=中心X+(半径*cos(rad));
浮动y1=中心Y+(半径*sin(rad));
浮点数opprad=rad+PI;
浮动x2=中心x+(半径*cos(opprad));
浮动y2=中心Y+(半径*sin(opprad));
strokeCol+=冲程变化;
如果(冲程>354){
冲程变化=-1;
}否则如果(strokeCol<0){
冲程变化=1;
}
冲程(strokeCol,60);
冲程重量(1);
线(x1,y1,x2,y2);
}
}

您从未使用过类级别的
centerX
centerY
变量。而是在
drawWaveClock()
函数中重新计算新的
centerX
centerY

float centerX = width/2 + (noise(xnoise)*100) - 50;
float centerY = height/2 + (noise(ynoise)*100) - 50;
这些都是从屏幕中心绘制的,因此波将在相同的位置结束


今后,请尝试将您的问题缩小到一个能说明问题的范围。此外,请使用适当的命名约定-例如,类以大写字母开头。祝你好运。

在你引用的代码中,我想不出一个替代变量/值来代替宽度/2和高度/2。明智的做法是创建4种不同类型的波形时钟并输入它们。我不确定如何使用数组创建这些,因为它似乎与我的代码不兼容。感谢您指出错误。float drawCenterX=centerX+(noise(xnoise)*100)-50;浮动drawCenterY=centerY+(噪声(Y噪声)*100)-50;//并相应地重命名drawWaveClock()方法的其余部分//centerX,centerY来自constructor@ZIqbal您向
WaveClock
构造函数输入的值是多少?原始
centerX
centerY
变量包含哪些值?不,您不需要创建4个单独的类。嗨,villares给出的答案最终有效。谢谢你们两位的建议
  void drawWaveClock() {
    radiusnoise += 0.005;
    radius = (noise(radiusnoise)*350) + 1;
    angnoise += 0.005;
    angle += (noise(angnoise)*6) - 3;
    if (angle > 360) {
      angle -= 360;
    } else if (angle < 0) {
      angle += 360;
    }

    xnoise += 0.01;
    ynoise =+ 0.01;
    float centerX = width/2 + (noise(xnoise)*100) - 50;
    float centerY = height/2 + (noise(ynoise)*100) - 50;

    float rad = radians(angle);
    float x1 = centerX + (radius*cos(rad));
    float y1 = centerY + (radius*sin(rad));

    float opprad = rad + PI;
    float x2 = centerX + (radius*cos(opprad));
    float y2 = centerY + (radius*sin(opprad));

    strokeCol += strokeChange;
    if (strokeCol > 354) {
      strokeChange = -1;
    } else if (strokeCol < 0) {
      strokeChange = 1;
    }
    stroke(strokeCol, 60);
    strokeWeight(1);
    line(x1, y1, x2, y2);
  }
}
float centerX = width/2 + (noise(xnoise)*100) - 50;
float centerY = height/2 + (noise(ynoise)*100) - 50;