CMSC 110 (Introduction to Computing)
Spring 2016 - Section 02
Assignment #04
Due by 11:40 am on Wednesday, March 23, 2016
Task: Design an interactive composition using classes. You should design a class to model an object of your choice. Non-animated instances of this, or any additional objects of your choice, should be incoporated into the sketch where appropriate. You have 3 options for how to implement the the object interactions below:
Options (choose one):
Requirements:
To get you started, create a class for your object using the following template:
/** Your header comments go here to describe the class */ class MyObjectNameGoesHere { // the object's x-coordinate int x; // the object's y-coordinate int y; // a parameter for tracking the elapsed lifetime of the object, which can be used for animating it int t = 0; /** for option 2, track whether object is being dragged */ boolean selected = false; /** Constructor * @param xCoordinate the initial x coordinate of the object * @param yCoordinate the initial y coordinate of the object */ MyObjectNameGoesHere (int xCoordinate, int yCoordinate) { ... } /** Draw the object. * This function should be called every iteration of draw() to display the object. */ void display() { // drawing code for your object, with an animation based on t ... } /** Advance the object's animation. * This function should be called every iteration of draw() to advance it's animation. */ void step() { // increment the time counter t++; ... } /** for option 2 */ void mousePressed() { // if mouse position is inside of object // set selected to true; // optionally start the pressed animation } /** for option 2 */ void mouseReleased() { // if mouse position is inside of object // set selected to false; // optionally start the normal animation } /** for option 2 */ void mouseDragged() { // if selected is true // set position of object to mouse position // optionally start the dragged animation } /** for option 3 */ void collided() { // when called start the collision animation. } }
You do not need to follow this template for your class exactly, it is merely a
suggestion. You must also create the controlling code that creates new
objects based on where the user clicks and drawing code that displays all of the
static and animated objects. Again, here is a template to get you started:
// An array of all the dynamically created objects MyObjectNameGoesHere[] myObjects; // An index into the myObjects array to show where to save the next new object. // Note that this index cannot ever be allowed to exceed MAX_NUM_OBJECTS; // instead it should cycle from 0 to MAX_NUM_OBJECTS and then be reset to begin again at 0. int idxMyObjects = 0; /** Processing's setup function */ void setup() { size(500,500); // The maximum number of dynamic objects that can be displayed at once int MAX_NUM_OBJECTS = 20; myObjects = new MyObjectNameGoesHere[MAX_NUM_OBJECTS]; // For Option 2 only // create each of your MAX_NUM_OBJECTS objects at a prespecified // location and store them into the myObjects array ... // these ellipses mean your own code will follow } /** The main drawing loop for Processing */ void draw() { background(255); // NOTE: you are welcome to change this background // use a for loop to display all of the objects and advance their animations // NOTE: When writing this loop, you will need to test each element // of the myObjects array to see if it is an object or is empty. // Use the following code to test for an object: // if (myObjects[i] != null) { ... } // This example uses i as the loop variable; your implementation may // use something different. ... }The mouse interaction for option 1 and 3 could use something like the following:
/** for option 1 or option 3 */ void mousePressed() { // create a new object based on the location clicked // and store it into myObjects[idxMyObjects] ... // increment the index into myObjects, keeping it in the range 0...myObjects.length // NOTE: Make sure you understand how this line of code works! // It is complete as-is. You don't need to do anything else // to make the idxMyObjects counter work correctly. idxMyObjects = (idxMyObjects+1) % myObjects.length; }The mouse interaction for option 2 could use something like the following:
/** for option 2 */ void mousePressed() { // use a for loop to call mousePressed() on all of the objects } /** for option 2 */ void mouseReleased() { // use a for loop to call mouseReleased() on all of the objects } /** for option 2 */ void mouseDragged() { // use a for loop to call mouseDragged() on all of the objects }
Be sure to note that you must fill in code wherever you are instructed,
but you will certainly need to add other
variables and functions as well. Be creative and artistic!
Hints:
What to produce: