You don't put an
if inside a statement / method call. Java / Processing has a form for that, but I won't confuse you by presenting it.
if is a statement by itself,
You replace a normal statement with one with the
if, so that the code inside is executed only IF the condition is true.
It is pure logic:
if (condition)
[then] { execute statements inside the braces; }
else { execute other statements; }
The [then] isn't to type, it is here only to show the logic in English.
You can do something like: if (x + y > 200) { stroke(random(0, 127)); } else { stroke(random(128, 255); }
x + y > 200 is an example of condition.
The else is necessary to change back the stroke() that otherwise would apply to the next loops, whatever the condition.
Applied to your sketch:
- int x, y, numberoflines, size, spacing;
-
- void setup() {
- size (200, 200);
-
- y = height/2;
- size = 20;
-
- numberoflines = 10;
- spacing = width / numberoflines;
- println(spacing);
- }
-
- void draw () {
- background (255);
-
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- if (x + y > 200) {
- stroke(random(0, 127));
- } else {
- stroke(random(128, 255));
- }
- point(x, y);
- }
- }
- }
Note that most of the code in setup() isn't used in draw()...
If you need a switch, you can add one before the point() call:
- switch (x)
- {
- case 2:
- case 22:
- case 122:
- stroke(#00FF00);
- break;
- case 100:
- stroke(#0000FF);
- break;
- }
Change the value in the switch, eg. x + y, change the values of the cases, add some, etc. Play with the code.