Processing Examples - Basics

Moderator: BenChang

Processing Examples - Basics

Postby BenChang » Tue Nov 04, 2014 1:04 pm

Template
Use this as a starting template for Processing sketches
Code: Select all
// global variables
// declare variables here you want to use throughout the sketch

// the setup function is run once at the start
void setup()
{
   // set the size you want for the Processing window
   size(600,400);
}

// the draw function is run every frame
// this is the main "game loop" function
void draw()
{
   
}

User avatar
BenChang
 
Posts: 176
Joined: Mon Jan 17, 2011 4:48 pm

Re: Processing Examples - Basics

Postby BenChang » Tue Nov 04, 2014 1:04 pm

RandomLines
Code: Select all
void setup ()
{
  size(400,400);
  background(255);
}

void draw ()
{
  if (mousePressed==true)
  {
    background(255);
    color(0);
    strokeWeight(2);
   
    for (int i=0;i<100;i++)
    {
      line(random(400),random(400), random(400), random(400));
    }
  }
}

void keyPressed ()
{
   if (key == 's')
   {
     saveFrame("lines_####.jpg");
   }
}
User avatar
BenChang
 
Posts: 176
Joined: Mon Jan 17, 2011 4:48 pm

Re: Processing Examples - Basics

Postby BenChang » Tue Nov 04, 2014 1:04 pm

FadeInOut
Code: Select all
// template sketch

// the setup function is run once at the beginning

// int means integer, or whole number
// declaring the variable up here makes it 'global'

int c;    // color
boolean  goingUp;  // true if increasing, false if decreasing
void setup ()
{
   size (800,600);
   background(255,255,255);
   c=0;  // start with black
   goingUp=true;
   
}

// the draw function is run every frame
void draw ()
{
    fill (c,c,c);
    rect (300,100,300,400);
   
    if (goingUp)
    {
      c=c+1;
    }
    else
    {
      c=c-1;
    }
   
    if (c>255)
    {
       goingUp = false;
    }
   
    if (c<0)
    {
       goingUp = true;
    }
}
User avatar
BenChang
 
Posts: 176
Joined: Mon Jan 17, 2011 4:48 pm

Re: Processing Examples - Basics

Postby BenChang » Tue Nov 04, 2014 1:04 pm

FadeInOut2

Fade and foreground and background as opposites
Code: Select all
int c;    // color
int colorChange; // amount to change color by each frame

void setup ()
{
   size (800,600);
   background(255,255,255);
   c=0;  // start with black
   colorChange=20;     
}

// the draw function is run every frame
void draw ()
{
    background (255-c, 255-c, 255-c);
    fill (c,c,c);
    rect (300,100,300,400);
   
    c = c + colorChange;
   
    if (c>255 || c<0)
    {
       colorChange=-colorChange;
    }
}
User avatar
BenChang
 
Posts: 176
Joined: Mon Jan 17, 2011 4:48 pm

Re: Processing Examples - Basics

Postby BenChang » Tue Nov 04, 2014 1:05 pm

FadeInOut3
Fading boxes using a Class and an Array. The main sketch is named FadeInOut3. The class for the boxes is named FadingBox. In Processing, you create new class files using the "New Tab" option from the tabs popup menu.

FadeInOut3.pde
Code: Select all
// declare an array of 10 elements of type FadingBox
FadingBox [] boxes = new FadingBox[10];

void setup ()
{
   size (800,600);
   background(255,255,255);   
   
   for (int i=0;i<10;i++)
   {
       boxes[i] = new FadingBox (50,60*i,700,50,10-i);
   }
}

void draw ()
{
  for (int i=0;i<10;i++)
  {
     boxes[i].draw();
  }
}

FadingBox.pde
Code: Select all
class FadingBox
{
   // properties
   int x;
   int y;
   int w;
   int h;
 
   int c;  // color
   int colorChange;
   
   // constructor method
   FadingBox (int x,int y,int w,int h, int speed)
   {
     // "this" means "this object instance"
     
     this.x = x;
     this.y = y;
     this.w = w;
     this.h = h;
     this.colorChange = speed;
     
     this.c =0;
   }
   
   // draw method
   // NOT called automatically
   void draw ()
   {
      stroke(c,c,c);
      fill(c,c,c);
     
      rect (x,y,w,h);
     
      c = c + colorChange;
      if (c>255 || c<0)
      {
         colorChange *= -1;
      } 
   }
}
User avatar
BenChang
 
Posts: 176
Joined: Mon Jan 17, 2011 4:48 pm


Return to Art and Animation

Who is online

Users browsing this forum: No registered users and 1 guest

cron