Java I
Jonah Warrenjonah@parsons.edu
http://a.parsons.edu/~java2004
Lab 2.3 Answers
boolean isWhite=true; // keeps track of whether the background is white or black, start @ white
boolean prevPress=false; // keeps track of the previous "mousePressed" value, start at false
void setup() {
size(100,100); // create canvas
background(255); // make white background
}
void loop() {
// if mousePressed has changed from false to true (i.e. that it has changed from last time, and then new value is true)
if (mousePressed!=prevPress & mousePressed) {
// if it is white, change it to black (both background and isWhite value)
if (isWhite) {
background(0);
isWhite=false;
}
// if it is black, change it to white (both background and isWhite value)
else {
background(255);
isWhite=true;
}
}
// set the current mousePressed value to prevPress, next time around, it will be the previous value
prevPress=mousePressed;
}
boolean isWhite=true; // keeps track of whether the background is white or black, start @ white
void setup() {
size(100,100); // create canvas
background(255); // make white background
}
void loop() {
}
// this function gets executed when mouse is pressed... not a value being updated continously
void mousePressed() {
// change the background to white or black, depending on isWhite
if (isWhite) {
background(0);
}
else {
background(255);
}
// set isWhite to its opposite
isWhite=!isWhite;
}
int whichColor=0; // keeps track of color, start @ white
// 0 is white
// 1 is gray
// 2 is black
boolean prevPress=false; // keeps track of the previous "mousePressed" value, start at false
void setup() {
size(100,100); // create canvas
background(255); // make white background
}
void loop() {
// if mousePressed has changed from false to true (i.e. that it has changed from last time, and then new value is true)
if (mousePressed!=prevPress & mousePressed) {
// check what whichColor is: if its 0, set background to black, if 1 then gray, if 2 then white
if (whichColor==0) {
background(0);
}
else if (whichColor==1) {
background(127);
}
else {
background(255);
}
// increment which whichColor (use modulo 3 to keep value either 0,1 or 2)..
whichColor=(whichColor+1)%3;
}
// set the current mousePressed value to prevPress, next time around, it will be the previous value
prevPress=mousePressed;
}
int whichColor=0; // keeps track of color, start @ white
// 0 is white
// 1 is gray
// 2 is black
void setup() {
size(100,100); // create canvas
background(255); // make white background
}
void loop() {
}
// this function gets executed when mouse is pressed... not a value being updated continously
void mousePressed() {
// check what whichColor is: if its 0, set background to black, if 1 then gray, if 2 then white
if (whichColor==0) {
background(0);
}
else if (whichColor==1) {
background(127);
}
else {
background(255);
}
// increment which whichColor (use modulo 3 to keep value either 0,1 or 2)..
whichColor=(whichColor+1)%3;
}