Line won't keep still in the draw () method!!!!
in
Programming Questions
•
1 years ago
I created four patterns in a test sketch that did not display them in the draw() method. When I brought them into my main program to display them in the draw method, one pattern (which is just a still wavy line) became an animated pattern and it won't keep still!!! (even when I tell it too). Can someone help me with this?
RandomColorPalette myRandomColorPalette;
Stitches myCrossStitches;
Stitches myFlatStitches;
Stitches myDottedStitches;
Stitches myWavyStitches;
void setup() { // call the setup function to define initial enviroment properties
size(900, 700);
background(230);
myRandomColorPalette=new RandomColorPalette (color(255, 255, 255), 100, 0, 0);
myCrossStitches=new Stitches (color(227, 101, 221), 100, 600, 0, 10);
myFlatStitches=new Stitches (color(250, 100, 90), 100, 400, 0, 10, 6.6);
myDottedStitches=new Stitches (color(227, 208, 140), 100, 200, 0, 10, 4);
myWavyStitches=new Stitches (color(125, 225, 30), 100, 800, 0, 60.0, 40.0, 3.0);
} // end the setup function
void draw () { // call the draw function
background(230);
myRandomColorPalette.display();
myCrossStitches.display(10);
myFlatStitches.display(10, 6.6);
myDottedStitches.display(10, 4);
myWavyStitches.display(60.0, 40.0, 40.0);
} // end the draw function
abstract class Grid {
float sizeOfSquares;
color gridClr;
float boxPosX;
float boxPosY;
Grid(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
} // end of the constructor
void display () {
// initialization of quilt patterns (from Shiffman page 261)
for (int i=0;i<width;i+=sizeOfSquares) {
for (int j=0;j<height;j+=sizeOfSquares) {
fill(gridClr);
rect(i, j, sizeOfSquares, sizeOfSquares);
}
}
} // end of the display function
boolean mouseClickOverBox () {
if (mousePressed && ((mouseX>boxPosX) && (mouseX<boxPosX+sizeOfSquares) && (mouseY>boxPosY) && (mouseY<boxPosY+sizeOfSquares))) {
return true;
}
else {
return false;
}
}
}
class RandomColorPalette extends Grid {
RandomColorPalette(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
}
void display () {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
if (mouseClickOverBox()) {
gridClr=(color(random(1, 255), random(1, 255), random(1, 255)));
fill (gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
}
}
}
class Stitches extends RandomColorPalette {
float clrLine;
float strokeClr;
int stitchLength;
float spacing;
int dotSize;
float stitchOffset;
float stitchSpeed;
float stitchScalar;
float stitchAngle;
float stitchX;
// Constructor overloading for four different stitch patterns
// constructor for cross stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, int tempStitchLength) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempStitchLength= constrain (tempStitchLength, 8, 12);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
stitchLength=tempStitchLength;
stitchX=boxPosX+sizeOfSquares/2;
}
// constructor for flat stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, int tempStitchLength, float tempSpacing) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempStitchLength= constrain (tempStitchLength, 8, 12);
tempSpacing= constrain (tempSpacing, tempStitchLength/5, tempStitchLength*4/5);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
spacing=tempSpacing;
stitchLength=tempStitchLength;
stitchX=boxPosX+sizeOfSquares/2;
}
// constructor for dotted stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, int tempSpacing, int tempDotSize) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempSpacing= constrain (tempSpacing, 8, 12);
tempDotSize=constrain(tempDotSize, 1, 4);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
dotSize=tempDotSize;
spacing=tempSpacing;
stitchX=boxPosX+sizeOfSquares/2;
}
// constructor for the wavy stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, float tempStitchOffset, float tempStitchSpeed, float tempStitchScalar) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempStitchOffset=constrain (tempStitchOffset, 8, 12);
tempStitchSpeed=constrain(tempStitchSpeed, 1, 4);
tempStitchScalar=constrain(tempStitchScalar, 1, 4);
stitchAngle=90;
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
stitchOffset=tempStitchOffset;
stitchSpeed=tempStitchSpeed;
stitchScalar= tempStitchScalar;
stitchX=boxPosX+sizeOfSquares/2;
}
// Method overloading for display function of four different stitch patterns
// display () cross stitch
void display (int stitchLength) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchLength) {
stroke(0);
line (stitchX, i, stitchX+stitchLength, i+stitchLength);
line (stitchX, i+stitchLength, stitchX+stitchLength, i);
}
} // end the display () function
// display () flat stitch
void display (int stitchLength, float spacing) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchLength) {
stroke(0);
line (stitchX, i, stitchX, sizeOfSquares);
stroke (gridClr);
line (stitchX, i+spacing, stitchX, sizeOfSquares);
}
} // end the display () function
// display () dotted stitch
void display (int stitchLength, int dotSize) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchLength) { // loop that will use a counter that will draw a shape every 8 pixels for the whole
// height of the canvas
stroke (strokeClr); // determine the color of the stroke of the ellipses that form the dotted line as purple
fill (clrLine); // fill with purple the ellipses that form the dotted lines
ellipse (stitchX, i, dotSize, dotSize); // draw little ellipses every 8 pixels from top to bottom on the left of
// the canvas to make up the dotted line
} // end the for loop
} // end the display () function
// display () wavy stitch
void display (float StitchOffset, float StitchSpeed, float StitchScalar) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchScalar) {
float waveStitch=stitchOffset+cos(radians(stitchAngle))*stitchScalar;
stitchAngle+=stitchSpeed;
stroke(0);
strokeWeight (3);
fill (0);
point (stitchX+waveStitch, i);
} // end the for loop
} // end the display () function
} // end Stitches class
RandomColorPalette myRandomColorPalette;
Stitches myCrossStitches;
Stitches myFlatStitches;
Stitches myDottedStitches;
Stitches myWavyStitches;
void setup() { // call the setup function to define initial enviroment properties
size(900, 700);
background(230);
myRandomColorPalette=new RandomColorPalette (color(255, 255, 255), 100, 0, 0);
myCrossStitches=new Stitches (color(227, 101, 221), 100, 600, 0, 10);
myFlatStitches=new Stitches (color(250, 100, 90), 100, 400, 0, 10, 6.6);
myDottedStitches=new Stitches (color(227, 208, 140), 100, 200, 0, 10, 4);
myWavyStitches=new Stitches (color(125, 225, 30), 100, 800, 0, 60.0, 40.0, 3.0);
} // end the setup function
void draw () { // call the draw function
background(230);
myRandomColorPalette.display();
myCrossStitches.display(10);
myFlatStitches.display(10, 6.6);
myDottedStitches.display(10, 4);
myWavyStitches.display(60.0, 40.0, 40.0);
} // end the draw function
abstract class Grid {
float sizeOfSquares;
color gridClr;
float boxPosX;
float boxPosY;
Grid(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
} // end of the constructor
void display () {
// initialization of quilt patterns (from Shiffman page 261)
for (int i=0;i<width;i+=sizeOfSquares) {
for (int j=0;j<height;j+=sizeOfSquares) {
fill(gridClr);
rect(i, j, sizeOfSquares, sizeOfSquares);
}
}
} // end of the display function
boolean mouseClickOverBox () {
if (mousePressed && ((mouseX>boxPosX) && (mouseX<boxPosX+sizeOfSquares) && (mouseY>boxPosY) && (mouseY<boxPosY+sizeOfSquares))) {
return true;
}
else {
return false;
}
}
}
class RandomColorPalette extends Grid {
RandomColorPalette(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
}
void display () {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
if (mouseClickOverBox()) {
gridClr=(color(random(1, 255), random(1, 255), random(1, 255)));
fill (gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
}
}
}
class Stitches extends RandomColorPalette {
float clrLine;
float strokeClr;
int stitchLength;
float spacing;
int dotSize;
float stitchOffset;
float stitchSpeed;
float stitchScalar;
float stitchAngle;
float stitchX;
// Constructor overloading for four different stitch patterns
// constructor for cross stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, int tempStitchLength) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempStitchLength= constrain (tempStitchLength, 8, 12);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
stitchLength=tempStitchLength;
stitchX=boxPosX+sizeOfSquares/2;
}
// constructor for flat stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, int tempStitchLength, float tempSpacing) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempStitchLength= constrain (tempStitchLength, 8, 12);
tempSpacing= constrain (tempSpacing, tempStitchLength/5, tempStitchLength*4/5);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
spacing=tempSpacing;
stitchLength=tempStitchLength;
stitchX=boxPosX+sizeOfSquares/2;
}
// constructor for dotted stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, int tempSpacing, int tempDotSize) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempSpacing= constrain (tempSpacing, 8, 12);
tempDotSize=constrain(tempDotSize, 1, 4);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
dotSize=tempDotSize;
spacing=tempSpacing;
stitchX=boxPosX+sizeOfSquares/2;
}
// constructor for the wavy stitch
Stitches (color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY, float tempStitchOffset, float tempStitchSpeed, float tempStitchScalar) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
tempStitchOffset=constrain (tempStitchOffset, 8, 12);
tempStitchSpeed=constrain(tempStitchSpeed, 1, 4);
tempStitchScalar=constrain(tempStitchScalar, 1, 4);
stitchAngle=90;
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
stitchOffset=tempStitchOffset;
stitchSpeed=tempStitchSpeed;
stitchScalar= tempStitchScalar;
stitchX=boxPosX+sizeOfSquares/2;
}
// Method overloading for display function of four different stitch patterns
// display () cross stitch
void display (int stitchLength) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchLength) {
stroke(0);
line (stitchX, i, stitchX+stitchLength, i+stitchLength);
line (stitchX, i+stitchLength, stitchX+stitchLength, i);
}
} // end the display () function
// display () flat stitch
void display (int stitchLength, float spacing) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchLength) {
stroke(0);
line (stitchX, i, stitchX, sizeOfSquares);
stroke (gridClr);
line (stitchX, i+spacing, stitchX, sizeOfSquares);
}
} // end the display () function
// display () dotted stitch
void display (int stitchLength, int dotSize) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchLength) { // loop that will use a counter that will draw a shape every 8 pixels for the whole
// height of the canvas
stroke (strokeClr); // determine the color of the stroke of the ellipses that form the dotted line as purple
fill (clrLine); // fill with purple the ellipses that form the dotted lines
ellipse (stitchX, i, dotSize, dotSize); // draw little ellipses every 8 pixels from top to bottom on the left of
// the canvas to make up the dotted line
} // end the for loop
} // end the display () function
// display () wavy stitch
void display (float StitchOffset, float StitchSpeed, float StitchScalar) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchScalar) {
float waveStitch=stitchOffset+cos(radians(stitchAngle))*stitchScalar;
stitchAngle+=stitchSpeed;
stroke(0);
strokeWeight (3);
fill (0);
point (stitchX+waveStitch, i);
} // end the for loop
} // end the display () function
} // end Stitches class
1