diff --git a/java/examples/Topics/Create Shapes/BeginEndContour/BeginEndContour.pde b/java/examples/Topics/Create Shapes/BeginEndContour/BeginEndContour.pde index 6394d77b8..4c589b47e 100644 --- a/java/examples/Topics/Create Shapes/BeginEndContour/BeginEndContour.pde +++ b/java/examples/Topics/Create Shapes/BeginEndContour/BeginEndContour.pde @@ -11,7 +11,7 @@ void setup() { smooth(); // Make a shape s = createShape(); - s.noFill(); + s.fill(0); s.stroke(255); s.strokeWeight(2); // Exterior part of shape diff --git a/java/examples/Topics/Create Shapes/PolygonPShapeOOP3/Polygon.pde b/java/examples/Topics/Create Shapes/PolygonPShapeOOP3/Polygon.pde new file mode 100644 index 000000000..e775fb436 --- /dev/null +++ b/java/examples/Topics/Create Shapes/PolygonPShapeOOP3/Polygon.pde @@ -0,0 +1,34 @@ +// A class to describe a Polygon (with a PShape) + +class Polygon { + // The PShape object + PShape s; + // The location where we will draw the shape + float x, y; + // Variable for simple motion + float speed; + + Polygon(PShape s_) { + x = random(width); + y = random(-500, -100); + s = s_; + speed = random(2, 6); + } + + // Simple motion + void move() { + y+=speed; + if (y > height+100) { + y = -100; + } + } + + // Draw the object + void display() { + pushMatrix(); + translate(x, y); + shape(s); + popMatrix(); + } +} + diff --git a/java/examples/Topics/Create Shapes/PolygonPShapeOOP3/PolygonPShapeOOP3.pde b/java/examples/Topics/Create Shapes/PolygonPShapeOOP3/PolygonPShapeOOP3.pde new file mode 100644 index 000000000..c201db9c2 --- /dev/null +++ b/java/examples/Topics/Create Shapes/PolygonPShapeOOP3/PolygonPShapeOOP3.pde @@ -0,0 +1,63 @@ +/** + * PolygonPShapeOOP. + * + * Wrapping a PShape inside a custom class + * and demonstrating how we can have a multiple objects each + * using the same PShape. + */ + + +// A list of objects +ArrayList polygons; + +// Three possible shapes +PShape[] shapes = new PShape[3]; + +void setup() { + size(640, 360, P2D); + smooth(); + + shapes[0] = createShape(ELLIPSE,0,0,100,100); + shapes[0].fill(0,127); + shapes[0].stroke(0); + shapes[1] = createShape(RECT,0,0,100,100); + shapes[1].fill(0,127); + shapes[1].stroke(0); + shapes[2] = createShape(); + shapes[2].fill(0,127); + shapes[2].stroke(0); + shapes[2].vertex(0, -50); + shapes[2].vertex(14, -20); + shapes[2].vertex(47, -15); + shapes[2].vertex(23, 7); + shapes[2].vertex(29, 40); + shapes[2].vertex(0, 25); + shapes[2].vertex(-29, 40); + shapes[2].vertex(-23, 7); + shapes[2].vertex(-47, -15); + shapes[2].vertex(-14, -20); + shapes[2].end(CLOSE); + + + // Make an ArrayList + polygons = new ArrayList(); + + polygons = new ArrayList(); + + for (int i = 0; i < 25; i++) { + int selection = int(random(2)); // Pick a random index + Polygon p = new Polygon(shapes[selection]); // Use corresponding PShape to create Polygon + polygons.add(p); + } +} + +void draw() { + background(255); + + // Display and move them all + for (Polygon poly : polygons) { + poly.display(); + poly.move(); + } +} +