Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

deutsche Version

An example with a built in Screenshot-function

void setup()
{
  size(600,600);      // def. window size
  strokeWeight(1);    // line thickness 
}
  
void draw()
{
  background(255);    // def. background colour 
  
  for(int x = 0; x <= width; x+=30)
  {
    for(int y = 0; y <= height; y+=30)
    {
        smiley(x,y);          // funtion call
    }
  }
  
}
  
void keyPressed()
{
  switch(key)
  {
  case 's':
    save("screenShot.jpg");
    println("save the screen to screenShot.jpg");
    break;
  }
}
  
// function
void smiley(int x, int y)
{
  noFill();
  ellipse(x,y,18,18);  // head
  
  fill(0);
  ellipse(x - 3,y - 3,2,5);  // left eye
  ellipse(x + 3,y - 3,2,5);  // right eye 
  
  noFill();
  arc(x,y,10,10,radians(20),radians(180-20));  // mouth
}


This example doesn't have a screen output, it generates a PDF-File instead.

import processing.pdf.*;
  
void setup()
{
  size(600,600,PDF,"ornament.pdf");      // def. output resolution
  strokeWeight(1);    // line thickness 
}
  
void draw()
{
  background(255);    // def. background colour
  
  for(int x = 0; x <= width; x+=30)
  {
    for(int y = 0; y <= height; y+=30)
    {
        smiley(x,y);          // funtion call
    }
  }
  
  exit();
}
  
// function
void smiley(int x, int y)
{
  println("smiley");
  noFill();
  ellipse(x,y,18,18);  // head
  
  fill(0);
  ellipse(x - 3,y - 3,2,5);  // left eye 
  ellipse(x + 3,y - 3,2,5);  // right eye 
  
  noFill();
  arc(x,y,10,10,radians(20),radians(180-20));  // mouth
}

Exercise 7

  • Create a new shape instead of the smiley, and put exactly 100 repeations on the screen
  • Modify the colour of your shape with each repetition


Example Solution to Exercise 7 


int red;
int green;
int blue;
int myShapeSpacingX;
int myShapeSpacingY;
int offset = 30; // used to create an empty border around grid
void setup() {
  size(500, 500);
  myShapeSpacingX = (width-offset*2)/10; // offset is for left and right side
  myShapeSpacingY = (height-offset*2)/10; // offset is for top and bottom side
}

void draw() {
  background(255);
  for  (int i =0; i<=10; i++) {
    green+=10;
    for  (int j =0; j<=10; j++) {
      red+=10;
      fill(color(red, green, blue));
      myShape(offset+i*myShapeSpacingX, offset+j*myShapeSpacingY); 
    }
    red = 0;
  }
  green = 0;
}

void myShape(int x, int y) {
  ellipse(x, y, 30, 30);
  ellipse(x, y-15, 15, 15);
}