mirror of
https://github.com/processing/processing4.git
synced 2026-01-30 11:51:54 +01:00
This commit is contained in:
26
java/examples/Basics/Control/Conditionals1/Conditionals1.pde
Normal file
26
java/examples/Basics/Control/Conditionals1/Conditionals1.pde
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 1.
|
||||
*
|
||||
* Conditions are like questions.
|
||||
* They allow a program to decide to take one action if
|
||||
* the answer to a question is true or to do another action
|
||||
* if the answer to the question is false.
|
||||
* The questions asked within a program are always logical
|
||||
* or relational statements. For example, if the variable 'i' is
|
||||
* equal to zero then draw a line.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=10; i<width; i+=10) {
|
||||
// If 'i' divides by 20 with no remainder draw the first line
|
||||
// else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(153);
|
||||
line(i, 40, i, height/2);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, 20, i, 180);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Conditionals1 extends PApplet {
|
||||
public void setup() {/**
|
||||
* Conditionals 1.
|
||||
*
|
||||
* Conditions are like questions.
|
||||
* They allow a program to decide to take one action if
|
||||
* the answer to a question is true or to do another action
|
||||
* if the answer to the question is false.
|
||||
* The questions asked within a program are always logical
|
||||
* or relational statements. For example, if the variable 'i' is
|
||||
* equal to zero then draw a line.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=10; i<width; i+=10) {
|
||||
// If 'i' divides by 20 with no remainder draw the first line
|
||||
// else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(153);
|
||||
line(i, 40, i, height/2);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, 20, i, 180);
|
||||
}
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Conditionals1" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 1.
|
||||
*
|
||||
* Conditions are like questions.
|
||||
* They allow a program to decide to take one action if
|
||||
* the answer to a question is true or to do another action
|
||||
* if the answer to the question is false.
|
||||
* The questions asked within a program are always logical
|
||||
* or relational statements. For example, if the variable 'i' is
|
||||
* equal to zero then draw a line.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=10; i<width; i+=10) {
|
||||
// If 'i' divides by 20 with no remainder draw the first line
|
||||
// else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(153);
|
||||
line(i, 40, i, height/2);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, 20, i, 180);
|
||||
}
|
||||
}
|
||||
BIN
java/examples/Basics/Control/Conditionals1/applet/loading.gif
Normal file
BIN
java/examples/Basics/Control/Conditionals1/applet/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
26
java/examples/Basics/Control/Conditionals2/Conditionals2.pde
Normal file
26
java/examples/Basics/Control/Conditionals2/Conditionals2.pde
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 2.
|
||||
*
|
||||
* We extend the language of conditionals by adding the
|
||||
* keyword "else". This allows conditionals to ask
|
||||
* two or more sequential questions, each with a different
|
||||
* action.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=2; i<width-2; i+=2) {
|
||||
// If 'i' divides by 20 with no remainder
|
||||
// draw the first line else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(255);
|
||||
line(i, 40, i, height/2);
|
||||
} else if (i%10 == 0) {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, height/2, i, height-40);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Conditionals2 extends PApplet {
|
||||
public void setup() {/**
|
||||
* Conditionals 2.
|
||||
*
|
||||
* We extend the language of conditionals by adding the
|
||||
* keyword "else". This allows conditionals to ask
|
||||
* two or more sequential questions, each with a different
|
||||
* action.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=2; i<width-2; i+=2) {
|
||||
// If 'i' divides by 20 with no remainder
|
||||
// draw the first line else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(255);
|
||||
line(i, 40, i, height/2);
|
||||
} else if (i%10 == 0) {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, height/2, i, height-40);
|
||||
}
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Conditionals2" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Conditionals 2.
|
||||
*
|
||||
* We extend the language of conditionals by adding the
|
||||
* keyword "else". This allows conditionals to ask
|
||||
* two or more sequential questions, each with a different
|
||||
* action.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
|
||||
for(int i=2; i<width-2; i+=2) {
|
||||
// If 'i' divides by 20 with no remainder
|
||||
// draw the first line else draw the second line
|
||||
if(i%20 == 0) {
|
||||
stroke(255);
|
||||
line(i, 40, i, height/2);
|
||||
} else if (i%10 == 0) {
|
||||
stroke(153);
|
||||
line(i, 20, i, 180);
|
||||
} else {
|
||||
stroke(102);
|
||||
line(i, height/2, i, height-40);
|
||||
}
|
||||
}
|
||||
BIN
java/examples/Basics/Control/Conditionals2/applet/loading.gif
Normal file
BIN
java/examples/Basics/Control/Conditionals2/applet/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Embedding Iteration.
|
||||
*
|
||||
* Embedding "for" structures allows repetition in two dimensions.
|
||||
*/
|
||||
|
||||
float box_size = 11;
|
||||
float box_space = 12;
|
||||
int margin = 7;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Draw gray boxes
|
||||
|
||||
for (int i = margin; i < height-margin; i += box_space){
|
||||
if(box_size > 0){
|
||||
for(int j = margin; j < width-margin; j+= box_space){
|
||||
fill(255-box_size*10);
|
||||
rect(j, i, box_size, box_size);
|
||||
}
|
||||
box_size = box_size - 0.6;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class EmbeddedIteration extends PApplet {
|
||||
public void setup() {/**
|
||||
* Embedding Iteration.
|
||||
*
|
||||
* Embedding "for" structures allows repetition in two dimensions.
|
||||
*/
|
||||
|
||||
float box_size = 11;
|
||||
float box_space = 12;
|
||||
int margin = 7;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Draw gray boxes
|
||||
|
||||
for (int i = margin; i < height-margin; i += box_space){
|
||||
if(box_size > 0){
|
||||
for(int j = margin; j < width-margin; j+= box_space){
|
||||
fill(255-box_size*10);
|
||||
rect(j, i, box_size, box_size);
|
||||
}
|
||||
box_size = box_size - 0.6f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "EmbeddedIteration" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Embedding Iteration.
|
||||
*
|
||||
* Embedding "for" structures allows repetition in two dimensions.
|
||||
*/
|
||||
|
||||
float box_size = 11;
|
||||
float box_space = 12;
|
||||
int margin = 7;
|
||||
|
||||
size(200, 200);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Draw gray boxes
|
||||
|
||||
for (int i = margin; i < height-margin; i += box_space){
|
||||
if(box_size > 0){
|
||||
for(int j = margin; j < width-margin; j+= box_space){
|
||||
fill(255-box_size*10);
|
||||
rect(j, i, box_size, box_size);
|
||||
}
|
||||
box_size = box_size - 0.6;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
45
java/examples/Basics/Control/Iteration/Iteration.pde
Normal file
45
java/examples/Basics/Control/Iteration/Iteration.pde
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Iteration.
|
||||
*
|
||||
* Iteration with a "for" structure constructs repetitive forms.
|
||||
*/
|
||||
|
||||
int k;
|
||||
int xpos1 = 100;
|
||||
int xpos2 = 118;
|
||||
int count = 0;
|
||||
int timey = 0;
|
||||
int num = 12;
|
||||
|
||||
size(200, 200);
|
||||
background(102);
|
||||
noStroke();
|
||||
|
||||
// Draw gray bars
|
||||
fill(255);
|
||||
k=60;
|
||||
for(int i=0; i < num/3; i++) {
|
||||
rect(25, k, 155, 5);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
// Black bars
|
||||
fill(51);
|
||||
k = 40;
|
||||
for(int i=0; i < num; i++) {
|
||||
rect(105, k, 30, 5);
|
||||
k += 10;
|
||||
}
|
||||
k = 15;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(125, k, 30, 5);
|
||||
k +=10;
|
||||
}
|
||||
|
||||
// Thin lines
|
||||
k = 42;
|
||||
fill(0);
|
||||
for(int i=0; i < num-1; i++) {
|
||||
rect(36, k, 20, 1);
|
||||
k+=10;
|
||||
}
|
||||
66
java/examples/Basics/Control/Iteration/applet/Iteration.java
Normal file
66
java/examples/Basics/Control/Iteration/applet/Iteration.java
Normal file
@@ -0,0 +1,66 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Iteration extends PApplet {
|
||||
public void setup() {/**
|
||||
* Iteration.
|
||||
*
|
||||
* Iteration with a "for" structure constructs repetitive forms.
|
||||
*/
|
||||
|
||||
int k;
|
||||
int xpos1 = 100;
|
||||
int xpos2 = 118;
|
||||
int count = 0;
|
||||
int timey = 0;
|
||||
int num = 12;
|
||||
|
||||
size(200, 200);
|
||||
background(102);
|
||||
noStroke();
|
||||
|
||||
// Draw gray bars
|
||||
fill(255);
|
||||
k=60;
|
||||
for(int i=0; i < num/3; i++) {
|
||||
rect(25, k, 155, 5);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
// Black bars
|
||||
fill(51);
|
||||
k = 40;
|
||||
for(int i=0; i < num; i++) {
|
||||
rect(105, k, 30, 5);
|
||||
k += 10;
|
||||
}
|
||||
k = 15;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(125, k, 30, 5);
|
||||
k +=10;
|
||||
}
|
||||
|
||||
// Thin lines
|
||||
k = 42;
|
||||
fill(0);
|
||||
for(int i=0; i < num-1; i++) {
|
||||
rect(36, k, 20, 1);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "Iteration" });
|
||||
}
|
||||
}
|
||||
45
java/examples/Basics/Control/Iteration/applet/Iteration.pde
Normal file
45
java/examples/Basics/Control/Iteration/applet/Iteration.pde
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Iteration.
|
||||
*
|
||||
* Iteration with a "for" structure constructs repetitive forms.
|
||||
*/
|
||||
|
||||
int k;
|
||||
int xpos1 = 100;
|
||||
int xpos2 = 118;
|
||||
int count = 0;
|
||||
int timey = 0;
|
||||
int num = 12;
|
||||
|
||||
size(200, 200);
|
||||
background(102);
|
||||
noStroke();
|
||||
|
||||
// Draw gray bars
|
||||
fill(255);
|
||||
k=60;
|
||||
for(int i=0; i < num/3; i++) {
|
||||
rect(25, k, 155, 5);
|
||||
k+=10;
|
||||
}
|
||||
|
||||
// Black bars
|
||||
fill(51);
|
||||
k = 40;
|
||||
for(int i=0; i < num; i++) {
|
||||
rect(105, k, 30, 5);
|
||||
k += 10;
|
||||
}
|
||||
k = 15;
|
||||
for(int i = 0; i < num; i++) {
|
||||
rect(125, k, 30, 5);
|
||||
k +=10;
|
||||
}
|
||||
|
||||
// Thin lines
|
||||
k = 42;
|
||||
fill(0);
|
||||
for(int i=0; i < num-1; i++) {
|
||||
rect(36, k, 20, 1);
|
||||
k+=10;
|
||||
}
|
||||
BIN
java/examples/Basics/Control/Iteration/applet/loading.gif
Normal file
BIN
java/examples/Basics/Control/Iteration/applet/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Logical Operators.
|
||||
*
|
||||
* The logical operators for AND (&&) and OR (||) are used to
|
||||
* combine simple relational statements into more complex expressions.
|
||||
* The NOT (!) operator is used to negate a boolean statement.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(126);
|
||||
|
||||
boolean op = false;
|
||||
|
||||
for(int i=5; i<=195; i+=5) {
|
||||
// Logical AND
|
||||
stroke(0);
|
||||
if((i > 35) && (i < 100)) {
|
||||
line(5, i, 95, i);
|
||||
op = false;
|
||||
}
|
||||
|
||||
// Logical OR
|
||||
stroke(76);
|
||||
if((i <= 35) || (i >= 100)) {
|
||||
line(105, i, 195, i);
|
||||
op = true;
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "true"
|
||||
// The expression "if(op)" is equivalent to "if(op == true)"
|
||||
if(op) {
|
||||
stroke(0);
|
||||
point(width/2, i);
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "false"
|
||||
// The expression "if(!op)" is equivalent to "if(op == false)"
|
||||
if(!op) {
|
||||
stroke(255);
|
||||
point(width/4, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import processing.core.*;
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class LogicalOperators extends PApplet {
|
||||
public void setup() {/**
|
||||
* Logical Operators.
|
||||
*
|
||||
* The logical operators for AND (&&) and OR (||) are used to
|
||||
* combine simple relational statements into more complex expressions.
|
||||
* The NOT (!) operator is used to negate a boolean statement.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(126);
|
||||
|
||||
boolean op = false;
|
||||
|
||||
for(int i=5; i<=195; i+=5) {
|
||||
// Logical AND
|
||||
stroke(0);
|
||||
if((i > 35) && (i < 100)) {
|
||||
line(5, i, 95, i);
|
||||
op = false;
|
||||
}
|
||||
|
||||
// Logical OR
|
||||
stroke(76);
|
||||
if((i <= 35) || (i >= 100)) {
|
||||
line(105, i, 195, i);
|
||||
op = true;
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "true"
|
||||
// The expression "if(op)" is equivalent to "if(op == true)"
|
||||
if(op) {
|
||||
stroke(0);
|
||||
point(width/2, i);
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "false"
|
||||
// The expression "if(!op)" is equivalent to "if(op == false)"
|
||||
if(!op) {
|
||||
stroke(255);
|
||||
point(width/4, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
noLoop();
|
||||
}
|
||||
static public void main(String args[]) {
|
||||
PApplet.main(new String[] { "LogicalOperators" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Logical Operators.
|
||||
*
|
||||
* The logical operators for AND (&&) and OR (||) are used to
|
||||
* combine simple relational statements into more complex expressions.
|
||||
* The NOT (!) operator is used to negate a boolean statement.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
background(126);
|
||||
|
||||
boolean op = false;
|
||||
|
||||
for(int i=5; i<=195; i+=5) {
|
||||
// Logical AND
|
||||
stroke(0);
|
||||
if((i > 35) && (i < 100)) {
|
||||
line(5, i, 95, i);
|
||||
op = false;
|
||||
}
|
||||
|
||||
// Logical OR
|
||||
stroke(76);
|
||||
if((i <= 35) || (i >= 100)) {
|
||||
line(105, i, 195, i);
|
||||
op = true;
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "true"
|
||||
// The expression "if(op)" is equivalent to "if(op == true)"
|
||||
if(op) {
|
||||
stroke(0);
|
||||
point(width/2, i);
|
||||
}
|
||||
|
||||
// Testing if a boolean value is "false"
|
||||
// The expression "if(!op)" is equivalent to "if(op == false)"
|
||||
if(!op) {
|
||||
stroke(255);
|
||||
point(width/4, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
BIN
java/examples/Basics/Control/LogicalOperators/applet/loading.gif
Normal file
BIN
java/examples/Basics/Control/LogicalOperators/applet/loading.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
Reference in New Issue
Block a user