213
edits
|  (02.1.12 Processing sketch XII added) | |||
| Line 1,176: | Line 1,176: | ||
| is dividable by 3. For this the modulo operator % is very helpful.<br /> | is dividable by 3. For this the modulo operator % is very helpful.<br /> | ||
| [http://processing.org/reference/modulo.html modulo operator] | [http://processing.org/reference/modulo.html modulo operator] | ||
| == 02.01.2012 Processing sketches == | |||
| === XII Classes === | |||
| <source lang="java"> | |||
| /* GROWING FLOWERS (PROCESSING) | |||
|  *  | |||
|  * In this example the concept of "object  | |||
|  * orientated programming" (OOP) is used | |||
|  * to structure the code into objects by using | |||
|  * a class which describes a flower (a very  | |||
|  * abstract flower though). | |||
|  * | |||
|  * IMPORTANT: Make sure you have the corresponding  | |||
|  * class "Flower.pde" in your sketch folder !!! | |||
|  * | |||
|  * Frederic Gmeiner, 2012 | |||
|  */ | |||
| // declare two new objects of the type Flower and | |||
| // call their constructor function Flower() with | |||
| // individual values: | |||
| Flower flower1 = new Flower("Tulip", 200, 400); | |||
| Flower flower2 = new Flower("Rose", 0, 400); | |||
| void setup() { | |||
|   size(400, 400); | |||
|   smooth(); | |||
| } | |||
| void draw() { | |||
|   background(150); | |||
|   // call the grow() function of flower1 | |||
|   // which increases the growths by 0.1 | |||
|   flower1.grow(0.1); | |||
|   // call the paint() function of flower1 | |||
|   // in order to draw the flower to the screen | |||
|   flower1.paint(); | |||
|   // set the xPos variable of flower2 to the | |||
|   // current x position of the mouse | |||
|   flower2.xPos = mouseX; | |||
|   // call the grow() function of flower2 with | |||
|   // half of the value of flower1 | |||
|   flower2.grow(0.05); | |||
|   // call the paint function of flower2 | |||
|   flower2.paint(); | |||
|   // to make the objects interact with each other | |||
|   // we have to check from within our main draw loop. | |||
|   // here we check compare the individial x positions | |||
|   // and change the fill color accordingly: | |||
|   if ( flower2.xPos > flower1.xPos){ | |||
|     fill(255, 0, 0); | |||
|   } | |||
|   else { | |||
|     fill(150); | |||
|   } | |||
| } | |||
| </source> | |||
| <br /> <br /> | |||
| <source lang="java"> | |||
| /* FLOWER CLASS (PROCESSING) | |||
|  *  | |||
|  * Definition of the Flower class which we use in | |||
|  * the GROWING FLOWERS sketch. NOTE: Class names always  | |||
|  * start with an upper case letter. | |||
|  * | |||
|  * IMPORTANT: Make sure you have this class in the | |||
|  * file "Flower.pde" in your main sketch folder !!! | |||
|  * | |||
|  * Frederic Gmeiner, 2012 | |||
|  */ | |||
| class Flower { | |||
|   // global class variables | |||
|   float growth; | |||
|   String name; | |||
|   float xPos; | |||
|   float yPos; | |||
|   // this is the so called "constructor" method with which | |||
|   // the Flower class is initiated and fed with individual | |||
|   // values. | |||
|   Flower(String theName, int theXPos, int theYPos) {  | |||
|       // assign the local function variables (theName, theXPos, | |||
|       // theYPos) to the global class variables (name, xPos, yPos).  | |||
|      name = theName; | |||
|      xPos = theXPos; | |||
|      yPos = theYPos; | |||
|   } | |||
|   // function fow increasing the growth variable | |||
|   // (by means of a grow factor)  | |||
|   void grow(float theGrowFactor){ | |||
|       growth += theGrowFactor; | |||
|   } | |||
|   // function for painting the visual appearance | |||
|   // of the flower onto the screen | |||
|   void paint(){ | |||
|     line(xPos,yPos, xPos, yPos - growth); | |||
|     ellipse(xPos, yPos - growth, growth, growth); | |||
|   }  | |||
| } | |||
| </source> | |||
| <br /> <br /> | |||
| [[Category:Physical Computing]] | [[Category:Physical Computing]] | ||
| [[Category:Frederic Gmeiner]] | [[Category:Frederic Gmeiner]] | ||
| [[Category:WS11]] | [[Category:WS11]] | ||
edits