<< Chapter < Page | Chapter >> Page > |
int[] arrayDiInteri = new int[10];
if (i == NMAX) {
println("finished");}
else {i++;
}
int i = 0; //integer counter
while (i < 10) { //write numbers between 0 and 9
println("i = "+ i);
i++;
}
for (int i = 0; i < 10; i++) { //write numbers between 0 and 9
println("i = "+ i);
}
int MAX = 10;
float[] tabella = new float[MAX];
for (int i = 0; i < MAX; i++)
tabella[i] = random(1); //random numbers between 0 and 1
println(tabella.length + " elements:");
println(tabella);
Functions allow a modular approach to programming. In
Processing, in the
intermediate programming mode, we can define functions other than
setup()
and
draw()
, usable from
within
setup()
and
draw()
.
A function is characterized by the entities (with reference to the example ) :
int
)raddoppia
)i
)return 2*i
)A class is defined by a set of data and functions. An object is an instance of a class. Vice versa, a class is the abstractdescription of a set of objects.
Dot myDot;
void setup() {
size(300,20);
colorMode(RGB,255,255,255,100);
color tempcolor = color(255,0,0);
myDot = new Dot(tempcolor,0);
}
void draw() {
background(0);
myDot.draw(10);
}
class Dot
{
color colore;
int posizione;
//****CONSTRUCTOR*****//
Dot(color c_, int xp) {
colore = c_;
posizione = xp;
}
void draw (int ypos) {
rectMode(CENTER);
fill(colore);
rect(posizione,ypos,20,10);
}
}
A class is characterized by the following entities (with reference to the example ) :
Dot
)colore, posizione
)Dot()
)draw()
)An object (instance of a class) is declared in the same way as we declare a variable, but we have to allocate a space for it(as we did for the arrays) by means of its constructor (with reference to the example ).
Dot myDot;
)myDot = new Dot(tempcolor,0)
)myDot.draw(10);
)With the following
draw()
method we want to paint the window background with a gray
whose intensity depends on the horizontal position of themouse pointer.
void draw() {
background((mouseX/100)*255);}
However, the code does not do what it is expected to do. Why?
The variable
mouseX
is of
int
type, and the division it is subject to is of
the integer type. It is necessary to perform a
type
casting from
int
to
float
by means of the instruction
(float)mouseX
.
Notification Switch
Would you like to follow the 'Media processing in processing' conversation and receive update notifications?