/** * Name : ShapesDemo. * Version : 0.1. * Autor : Jorge Iván Meza Martínez . * Date : April 12 of 2008. * License : CC */ /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /** * This is the base class to all the shapes, it gives the position (x,y), * the dimension (width, height) and the abastract methods to draw and * to describe (toString) itself. */ abstract class Shape { protected int x; /* X position of the shape */ protected int y; /* Y position of the shape */ protected int width; /* width of the shape */ protected int height; /* heigth of the shape */ /* Constructor method */ public Shape(int _x, int _y, int _w, int _h) { x = _x; y = _y; width = _w; height = _h; } /* Gets a random color to be used by the inherited shapes */ protected color getRandomColor() { return color(random(255), random(255), random(255)); } /* Shapes must define how to be drawed */ public abstract void draw(boolean filled, int stroke_width); /* Shapes must define its own description */ public abstract String toString(); } /////////////////////////////////////////////////////////////////// /** Rectangle shape */ class RectShape extends Shape { public RectShape(int _x, int _y, int _w, int _h) { super(_x, _y, _w, _h); } public void draw(boolean filled, int stroke_width) { /* Sets the width of the line */ strokeWeight(stroke_width); /* Sets the mode of coordinates */ rectMode(CORNER); if(filled) /* Make the shape filled */ { noStroke(); fill(getRandomColor()); } else /* Make the shape outlined */ { noFill(); stroke(getRandomColor()); } /* Draw the rectangle */ rect(x, y, width, height); } /* Returns its own text description */ public String toString() { return "RectShape"; } } /////////////////////////////////////////////////////////////////// /** Ellipse shape */ class EllipseShape extends Shape { public EllipseShape(int _x, int _y, int _w, int _h) { super(_x, _y, _w, _h); } public void draw(boolean filled, int stroke_width) { /* Sets the width of the line */ strokeWeight(stroke_width); /* Sets the mode of coordinates */ ellipseMode(CORNER); if(filled) /* Make the shape filled */ { noStroke(); fill(getRandomColor()); } else /* Make the shape outlined */ { noFill(); stroke(getRandomColor()); } /* Draw the ellipse */ ellipse(x, y, width, height); } /* Returns its own text description */ public String toString() { return "EllipseShape"; } } /////////////////////////////////////////////////////////////////// /** Triangle shape */ class TriangleShape extends Shape { public TriangleShape(int _x, int _y, int _w, int _h) { super(_x, _y, _w, _h); } public void draw(boolean filled, int stroke_width) { /* Sets the width of the line */ strokeWeight(stroke_width); if(filled) /* Make the shape filled */ { noStroke(); fill(getRandomColor()); } /* Make the shape outlined */ else { noFill(); stroke(getRandomColor()); } /* Draw the triangle */ triangle(x+width/2, y, x, y+height, x+width, y+height); } /* Returns its own text description */ public String toString() { return "TriangleShape"; } } /////////////////////////////////////////////////////////////////// /** 4-sides shape */ class QuadShape extends Shape { public QuadShape(int _x, int _y, int _w, int _h) { super(_x, _y, _w, _h); } public void draw(boolean filled, int stroke_width) { /* Sets the width of the line */ strokeWeight(stroke_width); if(filled) /* Make the shape filled */ { noStroke(); fill(getRandomColor()); } else /* Make the shape outlined */ { noFill(); stroke(getRandomColor()); } /* Draw the four sides shape */ quad(x+width/2, y, x+width, y, x+width/2, y+height, x, y+height); } /* Returns its own text description */ public String toString() { return "QuadShape"; } } /////////////////////////////////////////////////////////////////// /** Random lines shape */ class LineShape extends Shape { public LineShape(int _x, int _y, int _w, int _h) { super(_x, _y, _w, _h); } public void draw(boolean filled, int stroke_width) { /* Sets the width of the line */ strokeWeight(stroke_width); for(int i=0; i<15; i++) { /* Calculate the random positions: starting point and ending of the lines */ int x1 = random(x, x+width); int y1 = random(y, y+height); int x2 = random(x, x+width); int y2 = random(y, y+height); /* Sets the width of the line */ stroke(getRandomColor()); /* Draw the line */ line(x1, y1, x2, y2); } } /* Returns its own text description */ public String toString() { return "LineShape: 15 random lines"; } } /////////////////////////////////////////////////////////////////// /** Random points shape */ class PointsShape extends Shape { public PointsShape(int _x, int _y, int _w, int _h) { super(_x, _y, _w, _h); } public void draw(boolean filled, int stroke_width) { /* Sets the width of the line */ strokeWeight(stroke_width); for(int i=0; i<50; i++) { /* Calculate the random positions of the points */ int x0 = random(x, x+width); int y0 = random(y, y+height); /* Sets the width of the line */ stroke(getRandomColor()); /* Draw the point */ point(x0, y0); } } /* Returns its own text description */ public String toString() { return "PointsShape: 50 random points"; } } /////////////////////////////////////////////////////////////////// /** Random polygon shape */ class PolygonShape extends Shape { public PolygonShape(int _x, int _y, int _w, int _h) { super(_x, _y, _w, _h); } public void draw(boolean filled, int stroke_width) { /* Sets the width of the line */ strokeWeight(stroke_width); if(filled) /* Make the shape filled */ { noStroke(); fill(getRandomColor()); } else /* Make the shape outlined */ { noFill(); stroke(getRandomColor()); } /* Calculates a random number of vertex */ int vertexCount = random(5, 15); /* Starts the shape definition */ beginShape(POLYGON); for(int i=0; i 0) ? stroke_width - 1 : 1; redraw(); break; } } /////////////////////////////////////////////////////////////////// void draw() { /* Set the font information */ PFont font = null; font = loadFont(FACE_SYSTEM, STYLE_PLAIN, SIZE_SMALL); textFont(font); /* Clear the screen with white background color */ background(255, 255, 255); /* If the indexed shape is available ... */ if(shapes[shape_index] != null) { /* Let the own shape to draw itself */ shapes[shape_index].draw(filled_shape, stroke_width); /* Draw the description message (at the bottom) */ fill(color(128, 156, 67)); text(shapes[shape_index].toString() + "; filled=" + ((filled_shape) ? "yes" : "no") + "; stroke weight=" + stroke_width, 20, height-20+3, width-40, 20); } else /* If the shape is not available show a message */ { fill(color(255, 0, 0)); text("This shape is not available. Very weird!", 20, height/2, width-40, 20); } } /////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////