Assignment 4

For this assignment I used the structure from assignment 3 but modified it to create triangles whenever the mouse button is pressed. The data comes from the xml file.

//global variable to store xml data
XMLElement xml;
CustomClass[] myTriangles;

//called on startup
void setup() {
  size(800, 800);
  //loads the xml file into an XMLElement object
  xml = new XMLElement(this, “boxData.xml”);

  //get the number of elements in the xml file
  int numTris = xml.getChildCount();
  myTriangles = new CustomClass[numTris];
  for (int i = 10; i < numTris; i++) {
    XMLElement tempChild = xml.getChild(i);
    //int tempZ = tempChild.getInt(“height”);
    //int tempW = tempChild.getInt(“width”);
    int tempX = tempChild.getInt(“xpos”);
    int tempY = tempChild.getInt(“ypos”);
    myTriangles[i] = new CustomClass(tempX,tempY);

void draw() {
    myTriangles = (CustomClass[])append(myTriangles, new CustomClass(mouseX,mouseY));
  for (int i = 6; i < myTriangles.length; i++) {
  for (int i = 10; i < myTriangles.length; i++) {
    for (int i = 150; i < myTriangles.length; i++) {


Custom Class:

class CustomClass {
  float cx;
  float cy;
  float velX;
  float velY;
  float cValue1;
  float cValue2;
  float cValue3;
  float cValue4;
  float cValue5;

  CustomClass (int cxIn, int cyIn)  
  //declare arguments here
    //store the necessary data from the arguments
    //in the appropriate data objects
    cx = cxIn;
    cy = cyIn;
    velX = (float)random(-8,6);
    velY = (float)random(-8,6);
    cValue1 = random(0,90);
    cValue2 = random(10,150);
    cValue3 = random(0,170);
    cValue4 = random(0,25);
    cValue5 = random(200,205);
    //call a function to draw the object on the screen
  }//end of constructor
  //define a function to perform the drawing on the screen
  void drawme()
    //perform the draw commands using class data objects
    //defined above
    stroke(cValue1, cValue2, cValue3, cValue4);
    fill(cValue1, cValue2, cValue3, cValue4);
    triangle(cx+250, cy*2, cx+150, cy+150, cx+(-150), cy+150);
  }//end of drawme function
  void updateme()
   cx += velX*1.5;
   cy += velY*2;




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s