Ich versuche, zufällige Farben jedes Mal, wenn eine Taste gedrückt wird, zu generieren. Ich nahm Code von diesem Beispiel: http://learningprocessing.com/examples/chp14/example-14-18-solar-system-OOPVerarbeitung: Hinzufügen von zufälligen Farben zu Ellipse in Klasse
Ich habe es für mein eigenes Projekt justiert, aber ich habe eine harte Zeit, die Füllung jedes Mal ändern, wenn eine Taste gedrückt wird. (Ich habe versucht, es hier einfügen, aber die Formatierung gehalten vermasselt, so dass ich es auf Pastebin)
Klasse File: https://pastebin.com/HiBXdA4A
Haupt Datei:
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));
}
}
}
Klasse Datei:
// 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();
}
}
Also ändern Sie die "planetColor" unter einer Bedingung, colorChange, aber Sie scheinen die colorChange Variable nicht zu ändern, und Sie verwenden nicht die Variable "planetColor" überall - es sei denn, ich vermisse etwas. – Stefan
Und wenn Sie herausgefunden haben, wie man colorChange einstellt, wenn eine Taste gedrückt wird, denken Sie daran, die Variable zurückzusetzen, wenn die PlanetColor ihren neuen Wert erhält. – Stefan