We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I understand the title may be a bit confusing but I'm extremely new to Processing so please bare with me.
I'm trying to generate random colors each time a key is pressed. I took code from this example: http://learningprocessing.com/examples/chp14/example-14-18-solar-system-OOP
I've tweaked it for my own project but I'm having a hard time changing the fill each time a key is pressed. All I'm getting is gray ellipses. Initially, I wanted to set a different color for each key [Q, W, E] but at this point, even random colors generated by the keys will do.
Main File:
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));
}
}
}
Class File
// 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();
}
}
Answers
****EDITED
Yes, you have to modify your code. I show my version below.
Notice I changed the content of keyEvent. Also I define a new method in your class (not really needed), I am calling fill(...) right before you draw the actual planet, I set noStroke() and the last and more important change, if you really want to have random colors, is to commented out the seed setting operation. However this has unwanted results that you need to address.
Kf
Thanks a lot for your reply! I'm getting an error saying there's a duplicate local variable c. Any suggestions?
The problem is line 83:
color c=color(random(255), random(255), random(255));
but c was already defined in prev lines.My bad, that was a last minute last minute change that I did....
Kf
Gotcha, I commented it out but still no color change.
I have edited my post and tested the code. It is now working. I made changes to lines 20 and 83.
To see the change of color, press any key from a to z.
Kf
Amazing, thank you so much for your help! I'll look over your code to make sure I fully understand what's going on.