We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello. I'm trying to make a program for my homework, in which it draws 1 to 7 skyscrapers in random locations and at random heights when the background under the rainbow is clicked, but when I call drawSkyscraper, the background within my circle changes color instead of the building appearing.
color[]rainbow=new color[7];
void setup()
{
size(600,600);
background(0,200,255);
noFill();
rainbow[0]=color(#FF0000);
rainbow[1]=color(#FF8633);
rainbow[2]=color(#FCFF33);
rainbow[3]=color(#58FF33);
rainbow[4]=color(#33ECFF);
rainbow[5]=color(#3933FF);
rainbow[6]=color(#C733FF);
}
void draw()
{
background(0,200,255);
drawRainbow();
//drawRainDrop(random(0,600),0,random(0,8),rainbow[1]);
//drawSkyscraper();
}
void drawRainbow()
{
strokeWeight(10);
stroke(rainbow[0]);
ellipse(300,410,800,800);
stroke(rainbow[1]);
ellipse(300,410,790,785);
stroke(rainbow[2]);
ellipse(300,410,780,770);
stroke(rainbow[3]);
ellipse(300,410,770,755);
stroke(rainbow[4]);
ellipse(300,410,760,740);
stroke(rainbow[5]);
ellipse(300,410,750,725);
stroke(rainbow[6]);
ellipse(300,410,740,710);
}
void drawRainDrop(float xLoc, int yLoc, float speed, color c)
{
//rainbow[c]=random(0,6);
int x=120;
int y=120;
int size=8;
yLoc=0;
//int xLoc=random(0,600);
noStroke();
for (int i = 0; i < 10; i++) {
fill(c);
ellipse(xLoc,yLoc + i*4,i*2,i*2);
xLoc=random(0,600);
speed=yLoc+speed;
}
}
void mouseClicked()
{
if(mouseX>0&&mouseX<600&&mouseY>=300)
{
drawSkyscraper();
}
else
{
}
}
void drawSkyscraper()
{
fill(0,4,74);
rect(50,300,50,200);
}
Answers
You're changing the
fill()
color in yourdrawSkyScraper()
function.Also note that you're calling
background()
inside yourdraw()
function, so any skyscraper you draw will immediately be drawn over.Okay. What's happening is that when you draw the skyscraper, yo set the filcolor to (0,4,74). You're doing this on line 78. This fill color doesn't go away. It remains set until it gets changed - and it doesn't get changed!
What you need to do is call
noFill();
inside yourdrawRainbow()
function, so that the ellipses you draw for the rainbow don't have the same fill color that was set for drawing the skyscraper.Once you fix this, your next question is obvious: Why doesn't my skyscraper remain drawn? Well, be cause you're only drawing it in mousePressed()... and it immediately gets drawn over by the call to background().
What you will need to do is to have variables that remembers how many and where the skyscrapers are, and then change those values in mousePressed(), and use those values to draw the skyscrapers inside draw().
You should do all your drawing in draw() (or functions that draw() calls)!
Thanks! Next, I want to draw the raindrops. When 10 of them are supposed to fall at random x locations and speeds, and all of the will be painted with a random color from the rainbow.
Alright.
How are you going to remember where each raindrop is?
How are you going to remember which color each one is?
What additional variables will you need?
Have you considered using classes/objects?
I'm going to use arrays for the rain drops location and speed. For color c, I'm going to use the rainbow array, and I'm going to need to define it within my method.
I'm not going to use classes/objects, since that wasn't really covered in my class.
Great! You should start by defining these arrays...
https://Forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays
There's a problem. When I try to draw a raindrop, it won't move down the screen despite me defining speed and yLoc, and also my color variable won't change color from purple, even though that's not the one I chose in the array.
I'm trying to see if all the raindrops fall at the same speed first.
Again, you are doing your drawing inside
mousePressed()
. So these raindrops will only be drawn for a fraction of a second when the mouse is pressed, and then they will immediately be drawn over on the next frame by the call to background(). This is not what you want.Do all your drawing in
draw()
!Example raindrop variables:
So should I just call mousePressed in draw then if want my skyscrapers and raindrops to appear? I actually called drawRaindrop in draw() and it looped the circles and they still didn't have the color I wanted them to have.
And in your example, how would any of those variables translate into the variables I'm using? Because you can't set the raindrop variables outside its method.
Keep in mind that I am only drawing one raindrop. To just draw one raindrop, I need 4 variables. They are the x and y positions of the drop, it's speed (how fast it is falling) and the raindrop's color.
Here, maybe this will make things clearer for you:
It's hard to see what variables you are using, because... I haven't seen you define any variables for your raindrops yet at all!
You said before that you would have arrays for your raindrops. This is a good idea. You will need four arrays. One for the x positions. One for the y positions. One for the speeds. And one for the colors...
I'm using the rainbow array for my colors, the same one I used to draw the rainbow.
color[]rainbow=new color[7];
For some reason, when I call it to replace color c, it doesn't work.
I was using
rainbow[1] would have colored the raindrop orange.
I tried to replicate your setup with a few additions and using my own variables color[]rainbow=new color[7];
But I still can't get those raindrops to move. In your code, I first modified your code so that it would draw a row of 10 raindrops and a randomly picked color, and it was able to do both.
So the question is: can I make my method do what it needs to do with the statements intact? Of course by that, that I mean XLoc, yLoc,etc.
said before that you would have arrays for your raindrops. This is a good idea. You will need four arrays. One for the x positions. One for the y positions. One for the speeds. And one for the colors...
For loop over these arrays to display the raindrops in draw- and also to move them by adding to x and y position in the array
float x[] = new int[10];
OK, so I defined my arrays, but I'm confused as to how to define my arrays and how to use the for loops.
Please read about for loops in the reference
Rain drop is independently from rainbow
Rain drop is just
ellipse (dropX[i],dropY[i],7,7);
then to move the drop
dropY[i] = dropY[i] + rainSPD[i];
press ctrl-t in processing
Double for-loop
in draw() you have
you loop over the raindrops. OK.
BUT in drawRainDrop() you loop again over the raindrops. Bad idea.
That's doing it twice (or in fact 9x9 times).
Solution
instead, in draw you want:
drawRainDrops();
without for loop [and as a plural]the function
drawRainDrops
looks like this [it's basically like your function]:your
setup()
function can be much shorter using a for loop similar like above:Remark
Remember to hit ctrl-t in processing to get auto-format of your code.
I use it all the time
Best, Chrisir ;-)
Hey I know I'm pretty late about this, but thanks for the help guys! I was able to get the program to do exactly what I wanted it to do.
Here's the final product:
Thanks for sharing!
there are a few things that can be criticized here
Arrays and for-loops
first in setup():
you use arrays but you forgot that the beauty of arrays is that we can use for loops with arrays.
So this:
becomes
please note how we use i as an index for the array:
dropX[i]=
!in fact we can treat all your arrays for the drops in one for loop:
that makes your setup() a lot shorter and more easy to handle and to read.
same applies for skyX, skyY, skyWd, skyHt (see below!!!)
By the way
in the function skyScraper_0_reset() you do just this:
So why not call skyScraper_0_reset()from setup() instead of having all the lines in setup() like:
??
From several arrays to classes
you have the properties (x,y, its color, its speed) of one drop distributed over the arrays.
you have to look in all four arrays in the same line number of that array (its index) to collect the properties of one drop.
Instead we can teach processing what a drop is and define it like a new type of variable.
see
https://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
for discussion.
Try to rewrite your code using
class Drop
and objects of it in an arraydrops
.Call of drawRainDrop wrong
In draw() you draw the drops:
BUT
this line:
drawRainDrop(dropX[i], dropY[i], rainSPD[0], dropC[4]);
let's you call ALL drops with the speed of drop #0 and ALL drops with color of drop 4. That renders both arrays (for rainSPD and dropC) useless.
ALL raindrops 6 times!!!
Another error:
in function drawRainDrop
BUT in draw()_
oups!
So each raindrop is drawn 9 times...
in draw() just say drawRainDrop();
same is true for drawSkyscraper() !!!!!!!!!
New Version
A new version with a few (not all by far!) corrections:
Among other things you can switch rain on and off now with the mouse and the rain just continues. Each drop that leaves the screen gets a reset.
Warm regards, Chrisir ;-)