Array object doesn't work

Hi:

I used this example as a guide
https://processing.org/examples/arrayobjects.html

but my sketch hangs the Java windows and I have to close the window.

Turnout[] aTurnouts;
void setup()
{
  size(1024,500);
  aTurnouts = new Turnout[8];
  aTurnouts[1] = new Turnout(10,90,'L','R');
  aTurnouts[2] = new Turnout(10,200,'R','L');
  aTurnouts[3]= new Turnout(10,480,'R','R');
  aTurnouts[4]= new Turnout(100,20,'L','D');
  aTurnouts[5] = new Turnout(200,20,'L','U');
  aTurnouts[6]= new Turnout(300,20,'R','D');
  aTurnouts[7] = new Turnout(400,20,'R','U');

}
void draw()
{
  background(0);
  for (Turnout aTurnout : aTurnouts) {
    aTurnout.show(true);
  }
}

class Turnout
{
  int xx, yy;
  char tt,oo;
  int h=15;
  int w=15;
  Turnout (int x, int y, char t, char o)
  {
    xx=x;
    yy=y;
    tt=t;
    oo=o;
  }
  void show(boolean bt)
  {
    stroke(255);
    strokeWeight(4);
    noFill();
    if (oo=='U' || oo=='D')
    {
      rect(xx,yy,h,w);
      if (!bt){line(xx+h/2,yy,xx+h/2,yy+w);}
    }
    else
    {
      rect(xx,yy,w,h);
      if (!bt){line(xx,yy+h/2,xx+w,yy+h/2);}
    }
    
    if (bt)
    {
    
    // LEFT TURNOUT
    if (tt=='L')
    {
      // LEFT OR.
      if (oo=='L')
      {
        line(xx+w,yy+h/2,xx,yy+h);
      }
      // RIGHT OR.
      if (oo=='R')
      {
        line(xx,yy+h/2,xx+w,yy+h);
      }
      // DOWN OR.
      if (oo=='D')
      {
        line(xx+h/2,yy,xx+h,yy+w);
      }
      // UP OR.
      if (oo=='U')
      {
        line(xx+h/2,yy+w,xx,yy);
      }
      
    }
    // RIGHT TURNOUT
    if (tt=='R')
    {
      // LEFT OR.
      if (oo=='L')
      {
        line(xx+w,yy+h/2,xx,yy);
      }
      // RIFHT OR.
      if (oo=='R')
      {
        line(xx,yy+h/2,xx+w,yy);
      }
      // DOWN OR.
      if (oo=='D')
      {
        line(xx+h/2,yy,xx,yy+w);
      }
      // UP OR.
      if (oo=='U')
      {
        line(xx+h/2,yy+w,xx+h,yy);
      }
    }
    }
  }
}

If instead of using an array I create the objects as individual variables it will work (I copy the different code but the class definition is thesame):

Turnout t1 = new Turnout(10,10,'L','L');
Turnout t2 = new Turnout(10,90,'L','R');
Turnout t3 = new Turnout(10,200,'R','L');
Turnout t4 = new Turnout(10,480,'R','R');

Turnout t5 = new Turnout(100,20,'L','D');
Turnout t6 = new Turnout(200,20,'L','U');
Turnout t7 = new Turnout(300,20,'R','D');
Turnout t8 = new Turnout(400,20,'R','U');

void setup()
{
  size(1024,500);
}
void draw()
{
  background(0);
  t1.show(true);
  t2.show(true);
  t3.show(false);
  t4.show(false);
  t5.show(false);
  t6.show(false);
  t7.show(true);
  t8.show(false);
  
}
1 Like

The problem is in draw(). If I comment it, it does nothing but will not hang or freeze Processing.

aTurnouts[1] = new Turnout(10,90,'L','R');
aTurnouts[2] = new Turnout(10,200,'R','L');
aTurnouts[3]= new Turnout(10,480,'R','R');
aTurnouts[4]= new Turnout(100,20,'L','D');
aTurnouts[5] = new Turnout(200,20,'L','U');
aTurnouts[6]= new Turnout(300,20,'R','D');
aTurnouts[7] = new Turnout(400,20,'R','U');

That’s only 7. Where is aTurnouts[0]?

2 Likes

FIXED
yes I was trying to show element [0] that was not defined!

it was not a good idea to try Processing after a long working day…

Thanks

Now, is there anyway of adding mousePressed() event to each rectangle object so I can later walk the elements after a mousepress event and guess which one was pressed?