首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >(Java / Processing)如何在屏幕上的不同位置创建一个对象的多个实例?

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

Stack Overflow用户
提问于 2018-06-05 03:39:49
回答 1查看 44关注 0票数 2

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

有没有人能解释一下该怎么做呢?我认为这可能是waveClock类中的centerXcenterY变量的问题

代码语言:javascript
复制
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类

代码语言:javascript
复制
//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;

}

代码语言:javascript
复制
  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);
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-05 06:11:14

您永远不会使用类级别的centerXcenterY变量。相反,您将在drawWaveClock()函数中重新计算新的centerXcenterY

代码语言:javascript
复制
float centerX = width/2 + (noise(xnoise)*100) - 50;
float centerY = height/2 + (noise(ynoise)*100) - 50;

这些都是从屏幕的中心绘制的,所以波将在相同的位置结束。

以后,请尝试将您的问题范围缩小到演示该问题的MCVE。此外,请使用适当的命名约定-例如,类以大写字母开头。祝好运。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50687679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档