Processing: Adding random colors to ellipse in class -


i'm trying generate random colors each time key pressed. took code example: http://learningprocessing.com/examples/chp14/example-14-18-solar-system-oop

i've tweaked own project i'm having hard time changing fill each time key pressed. (i tried pasting here formatting kept messing up, put on pastebin)

class file: https://pastebin.com/hibxda4a

main file:

boolean colorchange = false;  // array of 8 planet objects planet[] planets = new planet[30]; arraylist<planet> newplanets = new arraylist<planet>() ;  void setup() {   //size(900, 900);   fullscreen();    // planet objects initialized using counter variable   (int = 0; < planets.length; i++ ) {      planets[i] = new planet(185 + i*5, 8);    }  }  void draw() {   background(0);    /* stars */   randomseed(103);    (int = 0; < 300; i++) {   float x = random(0, width);     float y = random(0, height);      ellipse(x, y, 2, 2);   fill(255);   }    // drawing earth   pushmatrix();   translate(width/2, height/2);   stroke(0);   fill(0, 191, 255);   ellipse(0,0,350,350);   nofill() ;   // drawing planets   (int = 0; < planets.length; i++ ) {     planets[i].update();     planets[i].display();   }      if(newplanets.size() > 0) {       for(int = 0 ; < newplanets.size() ;i++) {         println("newplanets should drawing") ;         planet p = newplanets.get(i) ;         p.update() ;         p.display() ;       }     }     popmatrix();    fill(255, 0, 0);   text("[press e air pollution]", width/9, height - (height/8));   fill(255, 255, 0);   text("[press w ground level pollution]", width/9, height - (height/8 + 15));   fill(0, 255, 0);   text("[press q greenhouse gasses]", width/9, height - (height/8 + 30)); }  void keypressed() {   if(key == 'q' || key == 'q') {       for(int = 0 ; < planets.length ; i++) {         newplanets.add(new planet(185 + i*5, 8));       }   }    if(key == 'w' || key == 'w') {       for(int = 0 ; < planets.length ; i++) {         newplanets.add(new planet(185 + i*5, 8)) ;       }   }    if(key == 'e' || key == 'e') {       for(int = 0 ; < planets.length ; i++) {         newplanets.add(new planet(185 + i*5, 8));       }   }  } 

class file:

// example 14-18: object-oriented solar system  class planet {   // each planet object keeps track of own angle of rotation.   float theta;      // rotation around sun   float diameter;   // size of planet   float distance;   // distance 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 angle rotate     theta += orbitspeed;   }    void display() {     // before rotation , translation, state of matrix saved 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 planet drawn, matrix restored popmatrix() next planet not affected.     popmatrix();   } } 

you shouldn't copy-paste code find on internet , try hammer away @ it. you'll give ton of headaches way. instead, need understand each line does, , create new sketch , take lessons accomplish goal.

you need break problem down smaller steps , take on steps 1 @ time. example, can create simple sketch shows random color? okay, can make color change whenever user types key?

the basic approach need take this:

  • store color in set of variables.
  • use variables draw scene.
  • change variables when user takes action.

that's general approach, , in case you'll want store variables in planet class each planet can have own color.

here's small example follows approach show random color whenever user clicks mouse:

<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>

this code example, shows how can use above approach change stuff when user takes action. highly recommend start on simple sketch this, , post mcve if stuck. luck.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -