Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 14-14: Rotating both squares

float theta1 = 0;
float theta2 = 0;

void setup() {
  size(200,200,P3D);
}

void draw() {
  background(255);
  stroke(0);
  fill(175);
  rectMode(CENTER);
  
  // Save the current transformation matrix. 
  // This is where we started, with (0,0) in the top left corner of the window and no rotation.
  pushMatrix(); 
  
  // Translate and rotate the first rectangle.
  translate(50,50); 
  rotateZ(theta1);
  // Display the first rectangle.
  rect(0,0,60,60); 
  // Restore matrix from Step 1 so that it isn't affected by Steps 2 and 3!
  popMatrix(); 
  
  pushMatrix();
  // Translate and rotate the second rectangle.
  translate(150,150); 
  rotateY(theta2);
  // Display the second rectangle.
  rect(0,0,60,60); 
  popMatrix();
  
  theta1 += 0.02;
  theta2 += 0.02;
}
  • Yahya

    A similar example, but demonstrating the effects of failing to push and pop the transformation matrices, might be instructive.