Week 8: Wor­k­i­n­g with Obj­e­c­t­s and Images

by Gawshika Aumkaran

Working with Images

  • Show Code
/* @pjs preload="/uploads/16114/img6441.jpg"; */
/** @peep sketch */
size(300, 300);
background(40);
PImage img;
// Image must be preloaded by the browser
img = loadImage("/uploads/16114/img6441.jpg");
image(img, 20, 20, 260, 260);

Tinting Images

  • Show Sketch
/* @pjs preload="/uploads/16114/img6441.jpg"; */
/** @peep sketchcode */
size(300, 300);
 
PImage img = loadImage("/uploads/16114/img6441.jpg");
tint(255, 0, 0);
image(img, 0, -200);
tint(0, 255, 0);
image(img, 100, -100);
tint(0, 0, 255);
image(img, 200, 0);

Transparent Tints

  • Show Sketch
/* @pjs preload="/uploads/16114/img6441.jpg"; */
/** @peep sketchcode */
size(300, 300);
PImage img = loadImage("/uploads/16114/img6441.jpg");
background(255);
tint(255, 51);
// Draw the image 4 times
for (int i = 0; i < 4; i++) {
    rotate(radians(-10));
  image(img, i*width/15-80, -250, 250);
}

Working with Objects (Circles)

  • Show Sketch
/** @peep sketchcode */
Circle[] circles;
 
void setup() {
  size(300, 300);
  colorMode(HSB, 360, 100, 100);
  circles = new Circle[100];
  for (int i = 0; i < circles.length/4; i++) {
    circles[i] = new Circle();
    circles[i].x = random(width);
    circles[i].y = random(height);
    circles[i].radius = random(10, 40);
    circles[i].colour = color(random(260), random(100), 100, 50 );
  }  
}
 
void draw() {
  noStroke();
  for (int i = 0; i < circles.length/4; i++) {
    fill(circles[i].colour);
    ellipse(circles[i].x, circles[i].y, 2*circles[i].radius, 2*circles[i].radius);
  }
}
 
class Circle {
  float x;
  float y;
  float radius;
  color colour;
}

Working with Objects (Squares)

  • Show Sketch
/** @peep sketchcode */
 
Square[] squares;
 
void setup() {
  size(300, 300);
  colorMode(HSB, 360, 100, 100);
  squares = new Square[100];
  for (int i = 0; i < squares.length/4; i++) {
    squares[i] = new Square()
    squares[i].x = random(width);
    squares[i].y = random(height); 
    squares[i].radius = random(10, 40);
    squares[i].colour = color(random(260), random(100), 100, 50);
  }
}
 
void draw() {
  for (int i = 0; i < squares.length/4; i++) {
    squares[i].draw();
  }
}
 
class Square {
  float x;
  float y;
  float radius;
  color colour;
 
  void draw() {
    noStroke();
    fill(colour);
    rect(x, y, 2*radius, 2*radius);
  }
}

Comments

Nobody has said anything yet.