This reference is for Processing 2.0+. If you have a previous version, use the reference included with your software. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Javadoc.

Name

createShape()

Examples
PShape square;  // The PShape object

void setup() {  
  size(100, 100, P2D);
  // Creating the PShape as a square. The corner 
  // is 0,0 so that the center is at 40,40 
  square = createShape(RECT, 0, 0, 80, 80);
}

void draw() {
  shape(square, 10, 10);
}

PShape s;  // The PShape object

void setup() {  
  size(100, 100, P2D);
  // Creating the PShape as a square. The corner 
  // is 0,0 so that the center is at 40,40 
  s = createShape();
  s.fill(0, 0, 255);
  s.noStroke();
  s.vertex(0, 0);
  s.vertex(0, 50);
  s.vertex(50, 0);
  s.end();
}

void draw() {
  shape(s, 25, 25);
}

PShape s;

void setup() {
  size(100, 100, P2D);
  s = createShape(TRIANGLE_STRIP);
  s.vertex(30, 75);
  s.vertex(40, 20);
  s.vertex(50, 75);
  s.vertex(60, 20);
  s.vertex(70, 75);
  s.vertex(80, 20);
  s.vertex(90, 75);
  s.end();
}

void draw() {
  shape(s, 0, 0); 
}

// Define the individual shapes and the group
PShape alien, head, body;

void setup() {
  size(100, 100, P2D);
  alien = createShape(GROUP);
  // Make two shapes  
  head = createShape(ELLIPSE, 0, 0, 50, 50);
  head.noStroke();
  body = createShape(RECT, 0, 45, 50, 40);
  body.fill(0);
  // Add the two "child" shapes to the parent group
  alien.addChild(body);
  alien.addChild(head);
}

void draw() {
  translate(25, 10);
  shape(alien);  // Draw the group
}
Description The createShape() function is used to define a new shape. Once created, this shape can be drawn with the shape() function. The basic way to use the function defines new primitive shapes. One of the following parameters are used as the first parameter: ELLIPSE, RECT, ARC, TRIANGLE, SPHERE, BOX, LINE. The parameters for each of these different shapes are the same as their corrsponding functions: ellipse(), rect(), arc(), triangle(), sphere(), box(), and line(). The first example above clarifies how this works.

Custom, unique shapes can be made by using createShape() without a parameter. After the shape is started, the drawing attributes and geometry can be set directly to the shape. See the second example above for specifics.

Geometry that groups vertices to build larger forms such as group of triangles can be created with createShape() using the same parameters as beginShape(). These options are POINTS, LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS, and QUAD_STRIP. See the third example above.

The createShape() function can also be used to make a complex shape made of other shapes. This is called a "group" and it's created by using the parameter GROUP as the first parameter. See the fourth example above to see how it works.

Syntax
createShape()
createShape(source)
createShape(type)
createShape(kind, p)
Parameters
type int: either POINTS, LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS, QUAD_STRIP
kind int: either LINE, TRIANGLE, RECT, ELLIPSE, ARC, SPHERE, BOX
p float[]: parameters that match the kind of shape
ReturnsPShape
Updated on December 7, 2012 08:53:38am PST

Creative Commons License