In my applet, when users type using a coded key (for instance, if they press on the SHIFT key to write a capital letter or if they type on BACKSPACE to erase a character, there is a square character that is typed.
Does anyone know how this can be fixed?
To see what I mean, try typing a capital letter or backspacing in this applet:
With the random function, I have created a simple algorithm to generate a different font type every time I run the program but the font flickers from one font type to the other when I run the program because draw () keeps calling back the random function.
class WordCollage extends InteractiveBoxes {
PFont font1, font2, font3, fontR;
String word1;
// constructor of the Picture Gallery class
WordCollage (int boxPosX, int boxPosY, int boxPosW, int boxPosH) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
void display () {
float rF =random(1, 3);
int randomFont=int(rF);
switch(randomFont) {
case 1:
fontR=font1;
break;
case 2:
fontR=font2;
break;
case 3:
fontR=font3;
break;
}
fill(0);
textFont(fontR, 48); // Set the font type and size at
text (word1, 700, 200);
}
}
I have created a box in which users can type in their comments as a response to an image I will show them on the screen.
My objective is to accumulate all the different users comments (sentences) in a text file and then later, randomly load some of the words in each user's comments and paste them on the canvas in random locations (so a bit like an "exquisite cadaver") to create a poetic word association.
Currently, I can save one comment in a text file by typing in my box and pressing RETURN, but if I type another comment, the first is overwritten by the second one (so I lose the first one).
I am assuming that the solution is to use an array list. Am I correct and if so, what direction should I take with this strategy to achieve my goal?
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
} // end the click function
void press (int mX, int mY) {
if (over==true) {
pressed=true;
boxPosXOff=mX-boxPosX;
boxPosYOff=mY-boxPosY;
}
} // end of the press function
void drag (int mX, int mY) {
if (pressed==true) {
boxPosX=mX-boxPosXOff;
boxPosY=mY-boxPosYOff;
}
} // end of the drag function
void release () {
pressed=false;
} // end of release function
}
class TextField extends InteractiveBoxes {
PFont fontA;
String userInputText= ""; // declare and initialize the variable to store text while it is being typed
String captureText= ""; // declare and initialize the variable to save typed text when return key is hit
String[] fullComment;
int spacing;
int fontHeight;
color boxClr;
// constructor of the TextField class
TextField (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color tempBoxClr) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
fontA = loadFont("ArialMT-48.vlw");
boxClr=tempBoxClr;
spacing = 5;
}
void display () {
// Display everything
fill (boxClr);
strokeWeight(4);
strokeJoin(ROUND);
rect (boxPosX, boxPosY, boxPosW, boxPosH);
fontHeight=16; // set the font size
textFont(fontA, fontHeight); // Set the font type and size at Arial 14 for text instructions
fill(240); // Set the fill for text instructions
text("Type a few words that describe your impression. \nHit return to save what you typed. ", boxPosX+spacing, boxPosY+fontHeight);
fontHeight=20; // reset the font size
fill (255); //set the fill for the user's input text
textFont(fontA, fontHeight); // Set the font type and size at Arial 20 for text instructions
text(userInputText, boxPosX+spacing, boxPosY+4*fontHeight+spacing);
text(captureText, boxPosX+spacing, boxPosY+4*fontHeight+spacing);
}
void keyPressed() {
fullComment = splitTokens(captureText, ", ");
userInputText = userInputText + key; // add each character typed by the user to the end of the userInputText String variable.
// If the return key is pressed, save the String and clear it
if (key == RETURN || key == ENTER) {
captureText = userInputText;
saveStrings("userCommentsFile.txt", fullComment);
userInputText = ""; // Clear the String by setting it equal to ""
}
}
}
I am new to coding and just made the worse coding architecture ever made so I apologize for the mess.
I am trying to get the color value from my myRandomColorPalette object to color the tile I will click on in myQuiltGrid by assigning it to the variable called tileClr. I think it's an easy get and set methods thing but I just don't know how to do it (I have tried different syntax and it does not work: see the commented out 2 bits of code in the RandomColorPalette class).
I also would like to rotate the tile pattern once it is on the my quilt grid (see void rotatePattern that is commented out in that same section).
QuiltGrid myQuiltGrid;
PFont fontInstructionsPlain; // create a variable to load the font for the instructions in plain font style
PFont fontInstructionsItalic; // create a variable to load the font for the instructions in italic font style
Instructions instruction;
PImage [] hawaiianPattern = new PImage [4]; // declare a variable that stores an array of quilting patterns images from clip art
void setup() { // call the setup function to define initial enviroment properties
size(900, 700); // set the canvas size at 800 pixels by 800 pixels
background(225, 225, 255); // set background color to light gray
myQuiltGrid= new QuiltGrid (color(255, 211, 232), 100, 200, 200);
fontInstructionsPlain=loadFont("SansSerif.plain-48.vlw"); // load the font used for the text in a data file attached to this file
fontInstructionsItalic=loadFont("SansSerif.italic-48.vlw"); // load the font used for the text in a data file attached to this file
textFont (fontInstructionsPlain, 16); // set the loaded font as the current one to use
for (int q = 0; q < hawaiianPattern.length; q++) { // create an array that loads the array of quilt patterns as numbered files
hawaiianPattern[q] = loadImage("pattern" + (q+1) + ".gif"); // load each quilt pattern individually based on its numbered file
} // end the for loop for loading the array of quilt patterns as numbered files
for (int q = 0; q < myPatternTile.length; q++) { // create an array that initializes each hawaiian pattern tile
myPatternTile[q]=new Pattern (color(255, 255, 255), 100, 0, 150+q*150, hawaiianPattern[q]);
}
for (int q = 0; q < myPatternTile.length; q++) { // create an array that loads the array of quilt patterns as numbered files
myPatternTile[q].showPatternTiles();
myPatternTile[q].tintPattern ();
// myPatternTile[q].rotatePattern();
}
instruction.write();
} // end the draw function
abstract class CanvasBoxes {
color boxClr;
float sizeOfSquares;
float boxPosX;
float boxPosY;
super (tempboxClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
boxClr=tempboxClr;
sizeOfSquares=tempsizeOfSquares;
quiltPattern=tempQuiltPattern;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
} // end of the constructor
void showPatternTiles() {
image (quiltPattern, boxPosX, boxPosY);
} // end of showPatternTiles () mode
void tintPattern () {
if (mouseClickOverBox()) {
tint (color(random(175, 255), random(175, 255), random(175, 255)));
}
}
} // end of Pattern class
} // end inner class (class inside QuiltGrid class)
boolean rollOverQuiltGrid() {
if (mouseX >= boxPosX && mouseY >= boxPosY) {
return true;
}
else {
return false;
}
} // end of rollOverQuiltGrid
void testMousePressed(int x, int y) {
if (mousePressed) {
for (int i=0; i < GridBoxes.size(); i++) {
if (x >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX) && x <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxW)) && y >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY) && y <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxH)))
((myQuiltBoxes)GridBoxes.get(i)).colorTheTile ();
}
}
if ((keyPressed) && (key == '1') && mousePressed) {
tileNumber=0;
for (int i=0; i < GridBoxes.size(); i++) {
if (x >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX) && x <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxW)) && y >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY) && y <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxH)))
((myQuiltBoxes)GridBoxes.get(i)).applyPatternTile();
}
}
if ((keyPressed) && (key == '2') && mousePressed) {
tileNumber=1;
for (int i=0; i < GridBoxes.size(); i++) {
if (x >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX) && x <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxW)) && y >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY) && y <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxH)))
((myQuiltBoxes)GridBoxes.get(i)).applyPatternTile();
}
}
if ((keyPressed) && (key == '3') && mousePressed) {
tileNumber=2;
for (int i=0; i < GridBoxes.size(); i++) {
if (x >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX) && x <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxW)) && y >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY) && y <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxH)))
((myQuiltBoxes)GridBoxes.get(i)).applyPatternTile();
}
}
if ((keyPressed) && (key == '4') && mousePressed) {
tileNumber=3;
for (int i=0; i < GridBoxes.size(); i++) {
if (x >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX) && x <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxX)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxW)) && y >= (((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY) && y <= ((((myQuiltBoxes)GridBoxes.get(i)).quiltBoxY)+(((myQuiltBoxes)GridBoxes.get(i)).quiltBoxH)))
((myQuiltBoxes)GridBoxes.get(i)).applyPatternTile();
}
}
}
void display () {
for (float i=boxPosX;i<width;i+=sizeOfSquares) {
for (float j=boxPosY;j<height;j+=sizeOfSquares) {
fill(boxClr);
myQuiltBoxes b = new myQuiltBoxes(i, j, sizeOfSquares, sizeOfSquares);
GridBoxes.add(b);
//((myQuiltBoxes)GridBoxes.get()).printme();
}
}
} // end of the display function
} // end of the Grid class
fill(boxClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
if (mouseClickOverBox()) {
boxClr=(color(random(125, 255), random(125, 255), random(125, 255)));
fill (boxClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
}
} // end of the display () function
} // end of the RandomColorPalette class
NOTE: I LEFT OUT TWO CLASSES OF MY CODE BECAUSE THEY DID NOT SEEM RELEVANT
I managed to come up with five stitching patterns that I was able to transform into drawing tools.
I am not entirely happy with the line stitch and double stitch but I like the dot stitch and the cross stitch.
Does anyone have an improvement to suggest for these two last ones? If you run my code, press the A key and drag the mouse for the first pattern; the B key and drag the mouse for the second pattern; the C key and drag the mouse for the third pattern; and the D key and drag the mouse for the fourth pattern.
Also, I'll need to make sure that they work against any colored backgrounds which means I'll need to get the pixel color of where they are at each moment.
Note: With the wavy stitch, I left it in the code but gave up altogether because the wave moves in draw() and I can't possibly see how it could be turned into a drawing tool anyway. It seems mathematically impossible to me.
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
// Method overloading for display function of different stitch patterns
// 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 () double flat stitch
void display (int stitchLength, float halfStitchLength, 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);
line (stitchX+stitchLength/2, i, stitchX+stitchLength/2, sizeOfSquares);
stroke (gridClr);
line (stitchX, i+spacing, stitchX, sizeOfSquares);
line (stitchX+stitchLength/2, i+spacing, stitchX+stitchLength/2, sizeOfSquares);
}
} // end the display () function
// 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);
}
}
// 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) {
stroke (strokeClr);
fill (clrLine);
ellipse (stitchX, i, dotSize, dotSize);
}
}
/* // display () wavy stitch
void display (float stitchSpeed, float stitchScalar) {
stroke(0);
fill(gridClr);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
for (float i=boxPosY; i<sizeOfSquares; i+=stitchScalar) {
float waveStitch=cos(radians(stitchAngle))*stitchScalar;
stitchAngle+=stitchSpeed;
stroke(0);
strokeWeight (3);
fill (0);
point (stitchX+waveStitch, i);
} // end the for loop
} // end the display () function */
// Method overloading for drawing tool of different stitch patterns
stroke(0);
line (pmouseX, pmouseY, mouseX, mouseY);
stroke (230);
line (pmouseX+spacing/3, pmouseY+spacing/3, mouseX, mouseY);
}
} // end the drawFlatStitch () function
// drawDoubleFlatStitch ()
void drawDoubleFlatStitch (int stitchLength, float halfStitchLength, float spacing) {
if ((keyPressed) && (key == 'b' || key == 'B') && mousePressed && (dist(pmouseX, pmouseY, mouseX, mouseY) >= stitchLength)) {
stroke(0);
line (pmouseX, pmouseY, mouseX+spacing, mouseY+spacing);
stroke (230);
line (pmouseX+spacing, pmouseY+spacing, mouseX, mouseY);
}
} // end the doubleFlatStitch () function
// drawCrossStitch ()
void drawCrossStitch (int stitchLength) {
if ((keyPressed) && (key == 'c' || key == 'C') && mousePressed && (dist(pmouseX, pmouseY, mouseX, mouseY) >= stitchLength)) {
stroke (0); // determine the color of the stroke of the ellipses that form the dotted line as purple
line (mouseX, mouseY, mouseX+stitchLength*3/4, mouseY+stitchLength*3/4);
line (mouseX, mouseY+stitchLength*3/4, mouseX+stitchLength*3/4, mouseY);
} // end the if condition block of code
} // end the drawDottedStitch () function
// drawDottedStitch ()
void drawDottedStitch (int stitchLength, int dotSize) {
if ((keyPressed) && (key == 'd' || key == 'D')&& mousePressed && (dist(pmouseX, pmouseY, mouseX, mouseY) >= stitchLength)) {
stroke (0); // determine the color of the stroke of the ellipses that form the dotted line as purple
fill (0); // fill with purple the ellipses that form the dotted lines
ellipse (mouseX, mouseY, dotSize, dotSize); // draw little ellipses every n pixels
} // end the if condition block of code
} // end the drawDottedStitch () function
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?
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
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
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
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
I am trying to recast a PImage into a class called Patterns. I am not familiar with the recasting syntax and I am getting a "maybe missing semicolon" (and it applies to the objects quiltPatterns). ^^^
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
} // end of the constructor
void display () {
for (float xPosSquare=0;xPosSquare<width;xPosSquare+=sizeOfSquares) {
for (float yPosSquare=0;yPosSquare<height;yPosSquare+=sizeOfSquares) {
fill(gridClr);
rect(xPosSquare, yPosSquare, sizeOfSquares, sizeOfSquares);
}
}
} // end of the display function
}
I have created four different stitching patterns. Is it a realistic goal to attempt to implement them into a drawing tool in processing? (what I mean by that is that when I would drag the cursor, the pattern could be drawn).
It seems to me unfeasible but maybe I just don't know where to start. Here are my patterns. The cross stitch is still unresolved.
void setup () {
size(900, 700);
background(230);
// this is the dotted line pattern
int spacing= 10;
for (float i=0; i<height; i+=spacing) {
int j=spacing*2/3;
strokeWeight (2);
stroke(0);
line (100, i, 100, height);
stroke (230);
line (100, i+j, 100, height);
}
When clicked, the white box should change to a randomized color. It works if I put the mouse events (rollover+click) in the subclass display method, but not when I put it in the superclass. I need it to be in the superclass because I will be extending several subclasses that will use this (boxes with images; box with patterns, etc,).
RandomColorPalette myRandomColorPalette;
void setup() { // call the setup function to define initial enviroment properties
size(900, 700); // set the canvas size at 800 pixels by 800 pixels
background(230); // set background color to light gray
myRandomColorPalette=new RandomColorPalette (color(227, 208, 140), 100, 400, 0);
} // end the setup function
void draw () { // call the draw function
background(230); // reset background color to light gray everytime the draw function is recalled
myRandomColorPalette.display();
} // end the draw function
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
I am getting a "the function display () does not exist" for my quiltpatterns tiles even though there is a display function in that subclass. What am I not getting?
PFont fontInstructions; // create a variable to load the font
PImage [] quiltPatterns = new PImage [4]; // declare a variable that stores an array of quilting patterns images from clip art
EmptyGrid myEmptyGrid;
void setup() { // call the setup function to define initial enviroment properties
size(900, 700); // set the canvas size at 800 pixels by 800 pixels
background(230); // set background color to light gray
fontInstructions=loadFont("CurlzMT-48.vlw"); // load the font used for the text in a data file attached to this file
textFont (fontInstructions); // set the loaded font as the current one to use
for (int q = 0; q < quiltPatterns.length; q++) { // create an array that loads the array of quilt patterns as numbered files
quiltPatterns[q] = loadImage("pattern" + (q+1) + ".gif"); // load each quilt pattern individually based on its numbered file
} // end the for loop for loading the array of quilt patterns as numbered files
myEmptyGrid= new EmptyGrid (color(227, 208, 140), 100);
} // end the setup function
void draw () { // call the draw function
background(230); // reset background color to light gray everytime the draw function is recalled
for (int q = 0; q < quiltPatterns.length; q++) { // create an array that loads the array of quilt patterns as numbered files
quiltPatterns[q].display();
} // end the for loop for loading the array of quilt patterns as numbered files
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
} // 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
}
// initialization of quilt patterns (from Shiffman page 261)
for (int i=200;i<width;i+=sizeOfSquares) {
for (int j=100;j<height;j+=sizeOfSquares) {
fill(gridClr);
rect(i, j, sizeOfSquares, sizeOfSquares);
}
}
} // end of the display function
} // end of the Grid class
class Instructions { // create a class for the instructions
// fields
String instruction1; // create a variable for the first line of the instructions that will appear on the screen
String instruction2; // create a variable for the second line of the instructions that will appear on the screen
String instruction3; // create a variable for the third line of the instructions that will appear on the screen
String instruction4; // create a variable for the fourth line of the instructions that will appear on the screen
String instruction5; // create a variable for the fifth line of the instructions that will appear on the screen
int textX; // create a variable for the X position of the text
int textY; // create a variable for the Y position of the text
I am trying to create a delay before the cars come back to their initial y position after they avoid the truck in this animation. Currently, I have the timer in the avoidance () method but it doesn't seem to work no matter where I put it. Also, my blue car behaves differently to the collision than the green one and I see now logical reason for this?
Vehicle(color tempC, float tempXpos, float tempYpos, float tempvwidth, float tempvheight, float tempXspeed) { // CFSR: create the constructor of the Vehicle class and declare its temporary variables
// collision detection method for the vehicle class: function that returns true or false based on whether the bigredtruck collides with another vehicle
boolean collDetect(Truck vcollision) {
if (xpos-vwidth/2<=(vcollision.xpos+vcollision.truckTotalWidth) && xpos+vwidth/2 >= vcollision.xpos && ypos-vheight/2<=(vcollision.ypos+vcollision.vheight/2) && ypos+vheight/2 >= (vcollision.ypos-vcollision.vheight/2))
{
return true;
}
else {
return false;
}
} // end of the collision detection method for the vehicle class
// avoidance mechanism method for the vehicle class
if (ypos-tavoidance.ypos>0) {
ydirection=ydirection*1;
}
else {
ydirection=ydirection*-1;
}
yspeed=20;
ypos = ypos+yspeed*ydirection;
} // end the avoidance mechanism function
} // end the block for the class called Vehicle
class Truck extends Vehicle {
// declare all of the properties for the subclass called Truck that are not properties of the superclass Vehicle
int strokeWeightTruck; // declare the variable that will be used to set a strokeweight for the objects of the truck class different than the strokeweight of the vehicle class
float truckCabinWidth; // declare the variable that will be used to determine the width of the cabin part of the truck
float spaceBtwCabinVan; // declare the variable that will be used to determine the space between the cabin and the van parts of the truck
float truckTotalWidth; // declare the variable that will be used to determine the total width of the cabin
float truckCabinHeight; // declare the variable that will be used to determine the height of the cabin part of the truck
float truckVanXCenter; // declare the variable that will be used to determine the middle of the van part of the truck to draw its rectangle in rect(CENTER) mode;
float angle; // declare the variable that will control the position of the sin wave for the truck object(s) to move
float truckMiddleYPos; // declare the variable that will determine the Y position from which the sin motion occurs when the truck object(s) will move
float truckYRangeOfMotion; // declare the variable that will determine the radius of the sin motion when the truck object(s) will move
float speedOfSinWaveMovement; // declare the variable that will impart a change in the angle that controls the sin wave for the truck object(s) to move
Truck (color tempC, float tempXpos, float tempYpos, float tempvwidth, float tempvheight, float tempTruckCabinWidth, float tempXspeed, int tempstrokeWeightTruck) { // create the constructor of the Truck class and declare its temporary variables
super (tempC, tempXpos, tempYpos, tempvwidth, tempvheight, tempXspeed);
// initialize the parameters for the constructor of the subclass called Truck
float angleCondition=fishAngle/180; // declare and create a variable that calculates the division of the angle of the cos and sin movement by 180 to determine the condition when the fish changes direction
int angleConditionConvertedtoInteger = int (angleCondition); // convert this variable from a float type to an integer type
if (angleConditionConvertedtoInteger%2==0) { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that if the remainder is an even number, the fish swims back
indecisiveFishReverse.move (); // move the reverse indecisive fish so that it looks like it is swimming back towards the start line
indecisiveFishReverse.display (); // draw the reverse indecisive fish so that it looks like it is swimming back towards the start line
}
else { // create a condition that uses the remainder of the division of this variable to determine the direction of the fish so that when the remainder is an odd number, the fish swims forward
indecisiveFish.move(); // move the indecisive fish in its forward direction so that it swims towards the finish line
indecisiveFish.display(); // draw the indecisive fish in its forward direction so that it swims towards the finish line
}
}
Fish (color clrFishtemp) { //constructor for the Fish class
clrFish=clrFishtemp;
fishAngleSpeed=0.4; // assign a value of 0.4 to the change of angle used in the cos and sign wave movement of the fish
fishCenterX=0.0; // assign a value of 0 to the variable that will determine the X position from which the cos motion occurs for the fish
fishCenterY=height/2; // assign a value of half the height of the canvas to the variable that will determine the Y position from which the sin motion occurs for the fish
sizeArcFishMoveX=width/3; // assign a value of a third of the width of the canvas to the variable that will determine the radius of the cos wave motion of the fish
sizeArcFishMoveY=height/3; // assign a value of a third of the height of the canvas to the variable that will determine the radius of the sin wave motion of the fish
}
// method to move the Fish
void move () {
fishX=fishLocX+speedXFish;
fishY=fishLocY;
fishLocX=fishCenterX+sizeArcFishMoveX*cos(radians(fishAngle)); // assign a cos wave movement to the variable that determines the X location of the fish in the animation
fishLocY=fishCenterY+sizeArcFishMoveY*sin(radians(fishAngle)); // assign a sin wave movement to the variable that determines the Y location of the fish in the animation
fishAngle+=fishAngleSpeed; // increment the angle of the fish cos and sin wave movements by 0.4
speedXFish+=(random(0.1, 0.8)); // assign a value that is a random value to move the fish horizontally across the screen
}