处理:在类中为椭圆添加随机颜色
问题描述:
我试图在每次按下某个键时生成随机颜色。我拿这个例子中的代码:http://learningprocessing.com/examples/chp14/example-14-18-solar-system-OOP处理:在类中为椭圆添加随机颜色
我已经调整过我自己的项目,但是我每次按下按键时都很难改变填充。 (我想在这里把它粘贴但格式不停地搞乱了,所以我把它放在引擎收录)
类文件: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();
}
}
答
你不应该复制粘贴在互联网上找到的代码,然后尝试去摧毁它。你会这样给自己一大堆头痛。相反,您确实需要准确理解每条线的功能,然后创建一个新草图并采取这些经验教训来实现您的目标。
您需要break your problem down into smaller steps,然后逐个采取这些步骤。例如,你可以创建一个简单的草图,只显示一个随机的颜色?好的,现在您可以在用户输入密钥时更改颜色了吗?
你需要采取的基本做法是这样的:
- 存放在一组变量的颜色。
- 使用这些变量绘制场景。
- 当用户采取某些操作时更改这些变量。
是一般的做法,并在你的情况下,你可能会想保存在您的Planet
类的变量,这样每个行星都可以有自己的颜色。
下面是遵循这一做法表现出随机颜色每当用户点击鼠标的一个小例子:
<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>
该代码仅仅是一个例子,但它说明了如何当用户采取行动时,使用上述方法来更改内容。我强烈建议你从这个简单的草图开始,如果你遇到困难,请发帖MCVE。祝你好运。
所以你在某些情况下改变了“planetColor”,colorChange,但是你似乎没有改变colorChange变量,并且你没有在任何地方使用“planetColor”变量 - 除非我失去了一些东西。 – Stefan
当您按下按键时已经发现如何设置colorChange,请记住在planetColor获得其新值时重置该变量。 – Stefan