Arraylist
in
Programming Questions
•
3 months ago
Hi, Im having a mess with arraylist. I´ve made an skech wich consist in a wheel whith some hooks in wich you can hung some weights. So when you push a button, the wheel start to round depending on the weights you hang.
It works, but I tried to put a restart button. So when you push, the weights arraylist is suposed to be erased, but its strange because only some weights are erasen, not all. The code that erase the weights is at the end.
This is the complete code:
class Pesa{ //weight
float masa;
int enganche;
float brazo;
float angle;
Pesa(int enganche0, float angle0, float masa0){
enganche = enganche0;
masa = masa0;
angle = angle0;
}
}
class Enganche{ //hook
float x;
float y;
Enganche(int x0, int y0){
x = x0;
y = y0;
}
}
Enganche[] enganches = new Enganche[9];
ArrayList pesas= new ArrayList();
PImage ImgRueda;
PImage ImgGancho;
PImage ImgPesa1;
PImage ImgPesa2;
PImage ImgPesa3;
PImage ImgPesa4;
PImage ImgBoton;
PImage ImgPulsado;
PImage ImgRestart;
PImage ImgRestartpulsado;
int ruedaSize = 600;
float angle = 0;
float w = 0.0;
float radius= 265;
float alpha = PI/3.4;
float difAngle = 2*PI/9;
int xpointer;
int ypointer;
float xpesa = 0;
float ypesa = 600;
float minercia;
float aceleracion;
float momento = 0;
boolean catched;
boolean marcha;
float masa;
float friccion =0.005;
boolean cargada;
void setup(){
size(900,700);
ImgRueda = loadImage("rueda.png");
ImgGancho = loadImage ("gancho.png");
ImgPesa1 = loadImage ("pesa1.png");
ImgPesa2 = loadImage ("pesa2.png");
ImgPesa3 = loadImage ("pesa3.png");
ImgPesa4 = loadImage ("pesa4.png");
ImgBoton = loadImage ("boton.png");
ImgPulsado = loadImage ("pulsado.png");
ImgRestart = loadImage ("restart.png");
ImgRestartpulsado = loadImage ("restartpulsado.png");
}
void draw(){
cursor(HAND);
background(200);
if (marcha){
aceleracion = momento/minercia;
w = (w + aceleracion)* (1-friccion);
}
angle = angle + w;
momento = 0;
//DIBUJAR RUEDA
pushMatrix();
rotate(angle);
translate(350*cos(angle) +350*sin(angle), 350*cos(angle)- 350*sin(angle));
image(ImgRueda, -ruedaSize/2,-ruedaSize/2,ruedaSize,ruedaSize);
popMatrix();
for (int i = 0;i<9; i++){
enganches[i] = new Enganche(0,0);
}
enganches[0].x = 350 + radius *cos(alpha + angle)-40;
enganches[0].y = 350 + radius * sin(alpha + angle)-20;
enganches[1].x= 350 + radius *cos(alpha + difAngle - 0.05 + angle)-35;
enganches[1].y = 350 + radius * sin(alpha + difAngle -0.05 + angle)-20;
enganches[2].x = 350 + (radius-10) *cos(alpha + 2 * difAngle - 0.2+ angle)-40;
enganches[2].y = 350 + (radius-10) * sin(alpha + 2 * difAngle - 0.2 + angle)-25;
enganches[3].x = 350 + (radius-20) *cos(alpha + 3 * difAngle - 0.15 + angle)-40;
enganches[3].y = 350 + (radius-20) * sin(alpha + 3 * difAngle -0.15 + angle)-20;
enganches[4].x = 350 + (radius-30) *cos(alpha + 4 * difAngle - 0.1+ angle)-40;
enganches[4].y = 350 + (radius-30) * sin(alpha + 4 * difAngle - 0.1 + angle)-20;
enganches[5].x = 350 + (radius-30) *cos(alpha + 5 * difAngle - 0.05+ angle)-40;
enganches[5].y = 350 + (radius-30) * sin(alpha + 5 * difAngle - 0.05 + angle)-25;
enganches[6].x = 350 + (radius-10) *cos(alpha + 6 * difAngle + angle)-40;
enganches[6].y = 350 + (radius-10) * sin(alpha + 6 * difAngle + angle)-25;
enganches[7].x = 350 + (radius-10) *cos(alpha + 7 * difAngle + angle)-40;
enganches[7].y = 350 + (radius-10) * sin(alpha + 7 * difAngle + angle)-20;
enganches[8].x = 350 + (radius-10) *cos(alpha + 8 * difAngle + angle)-35;
enganches[8].y = 350 + (radius-10) * sin(alpha + 8 * difAngle + angle)-25;
for(int i = 0; i<9;i++){
image(ImgGancho, enganches[i].x, enganches[i].y, 80,80);
}
//DIBUJA MUESTRARIO PESAS
image(ImgPesa1, 700, 10, 80,80);
image(ImgPesa2, 700, 100, 80, 80);
image(ImgPesa3, 700, 200, 80, 80);
image(ImgPesa4, 700, 300, 80, 80);
//DIBUJA LAS PESAS EN LOS GANCHOS Y CALCULA LOS MOMENTOS DE LAS FUERZAS
for(int i = 0;i < pesas.size();i++){
Pesa pesa = (Pesa) pesas.get(i);
if (pesa.masa == 20){
image(ImgPesa1, enganches[pesa.enganche].x,enganches[pesa.enganche].y + 40, 80,80);
}
if (pesa.masa == 30){
image(ImgPesa2, enganches[pesa.enganche].x,enganches[pesa.enganche].y + 40, 80,80);
}
if (pesa.masa == 40){
image(ImgPesa3, enganches[pesa.enganche].x,enganches[pesa.enganche].y + 40, 80,80);
}
if (pesa.masa == 60){
image(ImgPesa4, enganches[pesa.enganche].x,enganches[pesa.enganche].y + 40, 80,80);
}
if (marcha){
pesa.angle = pesa.angle + w;
}
momento = momento + pesa.masa * 265*cos(pesa.angle)/10;
}
//DIBUJA PESA EN EL AIRE
if (catched){
if (masa == 20){
image(ImgPesa1, mouseX-40, mouseY -20, 80,80);
}
if (masa == 30){
image(ImgPesa2, mouseX-40, mouseY -20, 80,80);
}
if (masa == 40){
image(ImgPesa3, mouseX-40, mouseY -20, 80,80);
}
if (masa == 60){
image(ImgPesa4, mouseX-40, mouseY -20, 80,80);
}
}
//BOTÓN MARCHA
image(ImgBoton,600,30,60,60);
if ((abs(mouseX - 630) < 30) && (abs(mouseY - 60) < 20)){
image(ImgPulsado,600,30,60,60);
}
//BOTÓN RESTART
image(ImgRestart,600,100,50,50);
if ((abs(mouseX - 630) < 30) && (abs(mouseY - 130) < 20)){
image(ImgRestartpulsado,600,100,50,50);
}
}
void mouseClicked(){
//COGE PESA
if ((abs(mouseX -(700 + 40))<40)&&(abs(mouseY -(10 + 40))<40)){
catched =true;
masa = 20;
}
if ((abs(mouseX -(700 + 40))<40)&&(abs(mouseY -(100 + 40))<40)){
catched =true;
masa = 30;
}
if ((abs(mouseX -(700 + 40))<40)&&(abs(mouseY -(200 + 40))<40)){
catched =true;
masa = 40;
}
if ((abs(mouseX -(700 + 40))<40)&&(abs(mouseY -(300 + 40))<40)){
catched =true;
masa = 60;
}
//ENGANCHA PESA
if (catched){
for(int i = 0;i < 9;i++){
if ((abs(mouseX -(enganches[i].x))<40)&&(abs(mouseY -(enganches[i].y + 40 ))<80)){
float hor = enganches[i].x-350;
float ver = enganches[i].y-350;
float angulo = 0;
if (hor > 0){
angulo = atan(ver/hor);
}
if (hor < 0){
angulo = PI - atan(ver/-hor);
}
pesas.add ( new Pesa(i,angulo, masa));
cargada = true;
catched = false;
}
}
}
//MARCHA
if (((abs(mouseX - 630) < 30) && (abs(mouseY - 30) < 60))&&(cargada)){
if (marcha == false){
marcha = true;
//CALCULA MOMENTO DE INERCIA. (PESO DE LA RUEDA DESPRECIABLE)
minercia = 0;
for(int i = 0;i < pesas.size();i++){
Pesa pesa = (Pesa) pesas.get(i);
float r = sqrt((enganches[pesa.enganche].x-350)*(enganches[pesa.enganche].x-350) + (enganches[pesa.enganche].y-350)* (enganches[pesa.enganche].y-350));
minercia = minercia + pesa.masa * r * r;
}
}
else{
marcha = false;
w = 0;
}
}
//RESTART
if (((abs(mouseX - 630) < 30) && (abs(mouseY - 130) < 60))&&(cargada)){
cargada = false;
marcha = false;
w = 0;
for(int i = 0;i < pesas.size();i++){
Pesa pesa = (Pesa) pesas.get(i);
pesas.remove(i);
}
}
}
2