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

时间:2018-06-04 19:39:49

标签: java processing

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

有人可以说明如何解决这个问题吗?我认为centerX类中的centerYwaveClock变量可能存在问题

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

waveClock类位于单独的标签页

//class is an architecture blueprint
//objects are the actual buildings built from the methods (can make as many as you like)
//constructor is the builder/constructor literally
class waveClock {
  float centerX; //co-ordinates of circle's position
  float centerY; //co-ordinates of circle's position
  float radius; //avg radius
 // float minRadius; //smallest size it can be
 // float maxRadius; //biggest size it can be
  color col; //colour
  int onsetThreshold; //
  int lowBand; //looks at lowest band of frequency and makes circle sensitive to it
  int highBand; //looks at highest band of frequency and makes circle sensitive to it
  boolean onset; //has there been an onset (beat has occurred or not?)

  //the constructor 
  waveClock(float x, float y, float r, color c, int lb, int hb, int t) {
    centerX = x;
    centerY = y;
    radius = r;
    col = c;
    lowBand = lb;
    highBand = hb; 
    onsetThreshold  = t;

}

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

1 个答案:

答案 0 :(得分:2)

您永远不会使用班级centerXcenterY变量。相反,您正在重新计算centerX函数中的新centerYdrawWaveClock()

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

这些都是从屏幕中心绘制的,因此波浪将最终处于相同的位置。

将来,请尝试将问题范围缩小到显示问题的MCVE。另外,请使用正确的命名约定 - 例如,以大写字母开头。祝你好运。