import java.util.*; //now that you are doing java you have to do your own importing import java.awt.*; public class RectangleTarget extends PApplet { //the magic line that makes it java ArrayList drops ; //keep an extendable list of Rectangle targetRect; //keeps four coords and some functions for testing void setup() { size(200,200); targetRect = new Rectangle(50,100,20,20); //make a new rectangle object drops = new ArrayList(); } void mouseDragged(){ if (targetRect.contains(mouseX,mouseY)){ //drag the rectangle around , change x and y, width and heigh are okay targetRect.x = mouseX - targetRect.width/2 ; targetRect.y = mouseY - targetRect.height/2 ; } } void mousePressed(){ Drop newDrop = new Drop(mouseX,mouseY); drops.add(newDrop); //arraylist automatically expands println("adding, drops size" + drops.size()); } void draw() { background(127); fill(50); //draw the rect regular rect(targetRect.x, targetRect.y, targetRect.width,targetRect.height); for (int i = drops.size()-1; i > -1; i--){ //go backwards through the list in class you have to delete one, //normally you would say for (int i =0; i< drops.size(); i++) to go forward throught an array Drop thisDrop = (Drop) drops.get(i); //it has amnesia when it comes out so you have to cast it as something thisDrop.moveIt(); thisDrop.drawIt(); if (thisDrop.isHitting(targetRect)){ //if it intersects the target, draw the target differently fill(200); rect(targetRect.x, targetRect.y, targetRect.width,targetRect.height); } if (thisDrop.isTooLow(height)){ drops.remove(i); //makes the list smaller println("removing, drops size" + drops.size()); } } } class Drop { Point dropPos = new Point(); //will hold both x and y Drop(int _x, int _y){ dropPos.x = _x; dropPos.y = _y; } void moveIt(){ dropPos.y = dropPos.y + 1; } boolean isTooLow(int _limit){ return (dropPos.y > _limit); } boolean isHitting(Rectangle _targetRect){ //before rectangles: return (dropPos.xpos < _targetMaxXPos && dropPos.xpos > _targetMInXPos && dropPos.Ypos < _targetMaxYPos && dropPos.ypos > _targetMinXPos); return (_targetRect.contains(dropPos)); //finally we don't have to have that terrible if statement } void drawIt(){ fill(20); ellipse( dropPos.x, dropPos.y,5,5); } } }