ArrayList delete issue
in
Programming Questions
•
8 months ago
I have been playing with a bit of code I found on this site.
It creates a few rectangles that you can move around, add to and delete.
With regards to deleting an object in the ArrayList, Im having some slight issues.
In keyPressed(), under case 'd' I get a exception when I try to delete an object.
Obviously I use the wrong syntax, but bugger me if I can spot the correct way.
Any suggestions ?
It creates a few rectangles that you can move around, add to and delete.
With regards to deleting an object in the ArrayList, Im having some slight issues.
In keyPressed(), under case 'd' I get a exception when I try to delete an object.
Obviously I use the wrong syntax, but bugger me if I can spot the correct way.
Any suggestions ?
//================= Entity CLASS BEGIN =========================================
class Entity
{
float x;
float y;
float destx;
float desty;
float speed;
float movingAngle;
boolean selected;
boolean moving;
//=========
public Entity()
{
x = width/2;
y = height/2;
speed = 15;
selected = moving = false; //loses focus (selected) after moving
}
//========
public void tick()
{
if (moving)
{
x += cos(movingAngle)*speed;
y += sin(movingAngle)*speed;
if (sqrt(sq(destx-x)+sq(desty-y)) <= speed)
{
moving = false; //stops moving at destination
}
}
// draw box with selected color dependin if box selected or not
if (selected)
{
stroke(255); //stroke param is colour in INT 0-255
fill(255, 0, 0); //fill param is v1, v2, v3 - float value
}
else
{
stroke(0);
fill(0, 100, 200);
}
rect(x, y, 50, 50); //size of drawn square
}
//========
public void goTo(float dx, float dy) {
destx = dx;
desty = dy;
movingAngle = atan2(dy-y, dx-x);
moving = true;
}
}
//================= Entity CLASS END ==========================================
//=============================================================================
ArrayList entities;
//=============================================================================
//=============================================================================
void setup() {
size(800, 800);
smooth();
background(128);
entities = new ArrayList();
for (int i = 0; i< 5;i++) // add the number of boxes in the loop (i) to the array list
{
entities.add(new Entity()); //adds a new box to the array (and screen)
}
}
//=============================================================================
void draw() {
background(0);
for (int i = 0; i< entities.size();i++) //size get the lenght of the array
{
Entity e = (Entity)entities.get(i);
e.tick(); //does the tick on each "e" instance
}
}
//=============================================================================
void mousePressed() {
//If no entity was pressed then we want them to move wherever the user clicked
boolean entityPressed = false;
//loop in reverse order so that if two square are on top of each other the one on top is selected
for (int i = entities.size()-1; i>= 0 ;i--)
{
Entity e = (Entity)entities.get(i);
if (mouseX > e.x && mouseX < e.x+50 && mouseY > e.y && mouseY < e.y+50)
{
entityPressed = true;
if (e.selected)
{
e.selected = false;
}
else
{
e.selected = true;
}
break; //break so that no other square are selected, this can be removed
}
}
//No entity was pressed - move all selected entities to where the user clicked and unselect them.
if (!entityPressed)
{
for (int i = 0; i< entities.size();i++)
{
Entity e = (Entity)entities.get(i);
if (e.selected)
{
e.goTo(mouseX, mouseY);
e.selected = false; // unselect after moving
}
}
}
}
void keyPressed ()
//pressing "a" on the keyboard, generates a new box
//press "d" and delete a box
//press "e" and it deletes all the objects in the arrayList
{
switch(key)
{
case 'a':
entities.add(new Entity());
break;
case 'd':
if (entities.size() > 1) //boundry check to ensure we do not delete past 1 object
{
//this doesn't work
int x = entities.lastIndexOf();
entities.remove(x);
//the below works, but removes the wrong element, ie always object 1 and not the last one added
// entities.remove(1);
}
else
{
break; //do nothing
}
break;
case 'e':
entities.clear();
break;
default:
break;
}
}
1