处理:在类中向椭圆添加随机颜色

时间:2017-08-15 02:44:08

标签: processing

每次按下按键时,我都会尝试生成随机颜色。我从这个例子中获取了代码:http://learningprocessing.com/examples/chp14/example-14-18-solar-system-OOP

我已经为自己的项目进行了调整,但每次按下按键时我都很难更改填充。 (我试着在这里粘贴它,但格式化仍然搞乱,所以我把它放在pastebin上)

班级档案:https://pastebin.com/HiBXdA4A

主档案

boolean colorChange = false;

// An array of 8 planet objects
Planet[] planets = new Planet[30];
ArrayList<Planet> newPlanets = new ArrayList<Planet>() ;

void setup() {
  //size(900, 900);
  fullScreen();

  // The planet objects are initialized using the counter variable
  for (int i = 0; i < planets.length; i++ ) {

    planets[i] = new Planet(185 + i*5, 8);

  }

}

void draw() {
  background(0);

  /* Stars */
  randomSeed(103);

  for (int i = 0; i < 300; i++) {
  float x = random(0, width);  
  float y = random(0, height);  

  ellipse(x, y, 2, 2);
  fill(255);
  }

  // Drawing the Earth
  pushMatrix();
  translate(width/2, height/2);
  stroke(0);
  fill(0, 191, 255);
  ellipse(0,0,350,350);
  noFill() ;
  // Drawing all Planets
  for (int i = 0; i < planets.length; i++ ) {
    planets[i].update();
    planets[i].display();
  } 

   if(newPlanets.size() > 0) {
      for(int i = 0 ; i < newPlanets.size() ;i++) {
        println("newPlanets should be drawing") ;
        Planet p = newPlanets.get(i) ;
        p.update() ;
        p.display() ;
      }
    }


  popMatrix();

  fill(255, 0, 0);
  text("[Press E for Air Pollution]", width/9, height - (height/8));
  fill(255, 255, 0);
  text("[Press W for Ground Level Pollution]", width/9, height - (height/8 + 15));
  fill(0, 255, 0);
  text("[Press Q for Greenhouse Gasses]", width/9, height - (height/8 + 30));
}

void keyPressed() {
  if(key == 'q' || key == 'Q') {
      for(int i = 0 ; i < planets.length ; i++) {
        newPlanets.add(new Planet(185 + i*5, 8));
      }
  }

  if(key == 'w' || key == 'W') {
      for(int i = 0 ; i < planets.length ; i++) {
        newPlanets.add(new Planet(185 + i*5, 8)) ;
      }
  }

  if(key == 'e' || key == 'E') {
      for(int i = 0 ; i < planets.length ; i++) {
        newPlanets.add(new Planet(185 + i*5, 8));
      }
  }

}

班级档案

// Example 14-18: Object-oriented solar system

class Planet {
  // Each planet object keeps track of its own angle of rotation.
  float theta;      // Rotation around sun
  float diameter;   // Size of planet
  float distance;   // Distance from sun
  float orbitspeed; // Orbit speed
  float resetingDistance ;
  color planetColor;
  boolean colorChange = false;


  Planet(float distance_, float diameter_) {
    distance = distance_;
    resetingDistance = distance_ ;
    diameter = diameter_;
    theta = 0;
    orbitspeed = random(0.01, 0.03);
    //planetColor = color( random(255), random(255), random(255), random(255));
  }

  void update() {
    // Increment the angle to rotate
    theta += orbitspeed;
  }

  void display() {
    // Before rotation and translation, the state of the matrix is saved with pushMatrix().
    pushMatrix(); 
    // Rotate orbit
    rotate(theta); 
    // Translate out distance
    translate(distance, 0); 
    stroke(0);
    fill(175);

    if (colorChange == true) {
      //fill(random(255), random(255), random(255), random(255));
      planetColor = color( random(255), random(255), random(255), random(255));
    }

    ellipse(0, 0, diameter, diameter);
    // Once the planet is drawn, the matrix is restored with popMatrix() so that the next planet is not affected.
    popMatrix();
  }
}

1 个答案:

答案 0 :(得分:0)

你不应该只是复制粘贴你在互联网上找到的代码,然后试着去挖掘它。你只会给自己带来很多麻烦。相反,你真的需要准确理解每条线的作用,然后创建一个新的草图,并将这些课程用于实现你的目标。

您需要break your problem down into smaller steps,然后逐个执行这些步骤。例如,您可以创建一个只显示随机颜色的简单草图吗?好的,现在只要用户键入一个键就可以进行颜色更改吗?

您需要采取的基本方法是:

  • 将颜色存储在一组变量中。
  • 使用这些变量绘制场景。
  • 当用户采取某些行动时更改这些变量。

这是一般方法,在您的情况下,您可能希望将这些变量存储在Planet类中,以便每个星球都有自己的颜色。

这是一个小例子,它遵循用户点击鼠标时显示随机颜色的方法:

&#13;
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.js"></script>
<script type="application/processing">

float r = 128;
float g = 128;
float b = 128;

void setup(){
   size(300, 200);
}

void draw(){
  background(r, g, b);
}

void mousePressed(){
  r = random(256);
  g = random(256);
  b = random(256);
}
</script>
<canvas> </canvas>
&#13;
&#13;
&#13;

此代码只是一个示例,但它显示了在用户执行操作时如何使用上述方法更改内容。我强烈建议您重新开始使用这样的简单草图,如果卡住,请发布MCVE。祝你好运。