What to do? (ArrayIndexOutOfBoundsExceptation)

edited November 2015 in Library Questions

For now i'm only trying to do some keyPressed to change music. But this is showing me an error, that i dont kow how tot fix it. Error name is "ArrayIndexOutOfBoundsExceptation:1".

<

pre lang="c"> import ddf.minim.*; import ddf.minim.analysis.*;

Minim minim; AudioPlayer[] som=new AudioPlayer[1];

void setup() { size(1280, 720);

minim = new Minim(this);

som[1]=minim.loadFile("C:/Users/User/Documents/Processing/brain_colors/som/Jason - Timeflip.wav"); som[2]=minim.loadFile("C:/Users/User/Documents/Processing/brain_colors/som/metal/Slipknot - The Devil In I.mp3");

for (int i =0; i<som.length; i++) {
som[i].play(); } }

void draw() { background(0);

//BRAIN SKETCH }

void keyPressed () { if (key == '0') exit(); else { if (key == '2') { som[2].play(); } } }

void stop() { for (int i =0; i<som.length; i++) {
som[i].play(); } { minim.stop(); } }

Tagged:

Answers

  • Answer ✓
    import ddf.minim.*; 
    import ddf.minim.analysis.*;
    
    Minim minim; 
    AudioPlayer[] som=new AudioPlayer[1];
    
    void setup() { 
      size(1280, 720);
    
      minim = new Minim(this);
    
      som[0]=minim.loadFile("C:/Users/User/Documents/Processing/brain_colors/som/Jason - Timeflip.wav"); 
      som[1]=minim.loadFile("C:/Users/User/Documents/Processing/brain_colors/som/metal/Slipknot - The Devil In I.mp3");
    
      for (int i = 0; i<som.length; i++) {
        som[i].play();
      }
    }
    
    void draw() { 
      background(0);
    
      //BRAIN SKETCH }
    
      void keyPressed () { 
        if (key == '0') exit(); 
        else { 
          if (key == '2') { 
            som[1].play();
          }
        }
      }
    
      void stop() { 
        for (int i =0; i<som.length; i++) {
          som[i].play();
        } 
        { 
          minim.stop();
        }
      }
    
  • Answer ✓

    all arrays in processing start with the index 0

    so 1st entry = 0

    so 2nd entry = 1

    I fixed lines 12 and 13 above

    the error

    the error is shown because in line 13 you had 2 which was higher than the maximum value of the array

  • Kk. Thank you for the help

  • Answer ✓

    also line 29

  • i've been changing my code, i've made some attempts for what i want now, but i fail in all. This is my last save before i do my tests.

    import java.io.File;
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    
    //BIBLIOTECA MUSICA
    
    Minim minim;
    AudioPlayer[] som = new AudioPlayer[5]; //NUMERO TOTAL DE MUSICAS
    AudioMetaData meta;
    
    PImage outline, play, pause; // IMAGEM COMANDO
    
    FFT fft;
    
    void setup() 
    { 
      size(1280, 720); //TAMANHO DA JANELA
    
      minim = new Minim(this);
    
      som[0]=minim.loadFile("C:/Users/Vasco/Documents/Processing/brainColors/data/som/Jason - Timeflip.wav"); 
      som[1]=minim.loadFile("C:/Users/Vasco/Documents/Processing/brainColors/data/som/metal/System of a down- Chop Suey!.mp3");
      som[2]=minim.loadFile("C:/Users/Vasco/Documents/Processing/brainColors/data/som/hardcore/Bratkilla & C-Netik - Corona Virus.mp3");
      som[3]=minim.loadFile("C:/Users/Vasco/Documents/Processing/brainColors/data/som/hardcore/The Supreme Team - Carnival of Doom.mp3");
    
      //IMAGEM COMANDO
      outline = loadImage("C:/Users/Vasco/Documents/Processing/brainColors/data/img/outline.png");
      play = loadImage("C:/Users/Vasco/Documents/Processing/brainColors/data/img/play.png"));
      pause = loadImage ("C:/Users/Vasco/Documents/Processing/brainColors/data/img/pause.png");
    }
    
    void draw() { 
      background(255);
    
      setupIcon();
    
      //BRAIN
    }
    
    void setupIcon() {
      image (outline, 16, 548);
      image (play, -10, 545);
      image (pause, 35, 549);
    }
    

    I'm uploading the img to try explain better what i'm trying to do. The img on the left shows what i've got so far, the one on the right shows what i'm trying. What i want for now is t put the name of the music playing and use the icons (play, pause), which is the eyes of the sketch to pause and play the music.

    01

  • Answer ✓

    this is a simple button example

    it printlns something when you press a button

  • edited November 2015 Answer ✓

    sorry, I posted the wrong code

  • ok, i'm going to try it

  • Answer ✓
    Button[] buts;
    String strText = "";
    
    void setup() {
      size(450, 200);
      colorMode(RGB);
      int unit = 40;
      int count;
      count = width/unit;
      buts = new Button[count];
      // init
      for (int x = 0; x < count; x++) {
        buts[x] = new Button(x*unit+6, 50, 25, color(255, 0, 0), color(0, 255, 0), color(0, 0, 255), x);
      }
    }
    
    void draw() {
      background(0, 0, 0);
      int i=0; 
      // display
      for (Button but : buts) {
        but.update();
        but.display();
        fill(0, 255, 0);
        text(str(i), 40*i+16, 40);
        i++;
      } // for-loop
      // show result text 
      text(strText, 15, height-15);
    } // func 
    
    void mousePressed() {
      for (Button but : buts) {
        but.press();
        if (but.pressed) {
          println(but.index);
          strText = str(but.index);
          break; // Other buttons won't be pressed, just stop
        }
      }
    }
    
    void mouseReleased() {
      for (Button but : buts) {
        but.release();
      }
    }
    
    //_____________________________//
    
    class Button {
      int x, y; // The x- and y-coordinates
      int size; // Dimension (width and height)
    
      // colors 
      color baseGray; // Default gray value
      color overGray; // Value when mouse is over the button
      color pressGray; // Value when mouse is over and pressed
    
      boolean over = false;    // True when the mouse is over
      boolean pressed = false; // True when the mouse is over and pressed
      int index;
    
      Button(int xp, int yp, 
        int s, 
        color b, 
        color o, 
        color p, 
        int index_) {
        x = xp;
        y = yp;
        size = s;
        baseGray = b;
        overGray = o;
        pressGray = p;
        index=index_;
      }
    
      // Updates the over field every frame
      void update() {
        if ((mouseX >= x) && (mouseX <= x + size) &&
          (mouseY >= y) && (mouseY <= y + size)) {
          over = true;
        } else {
          over = false;
        }
      }
    
      boolean press() {
        if (over == true) {
          pressed = true;
          return true;
        } else {
          return false;
        }
      }
    
      void release() {
        pressed = false; // Set to false when the mouse is released
      }
    
      void display() {
        if (pressed == true) {
          fill(pressGray);
        } else if (over == true) {
          fill(overGray);
        } else {
          fill(baseGray);
        }
        stroke(255);
        rect(x, y, size, size);
      }// method
    } // class 
    //
    
  • Thank you for all the help. but my teacher said i couldnt do any of my idea because some of the things i need to use, we dont give in class yet, and now i got to think in a new project, do a report and send all to him before 9.30am. Sorry for all the time you take with me in this failed project, ahahah. I hope I can count on your help in a next idea

  • Answer ✓

    you could do a simple image viewer

    show an image

    click a button, he goes to next image in folder etc.

  • I need to do something generative. I was thinking, in some geometric stuff (squares, lines, etc) in illustrator, save the images individually and make random stuff in processing.

    Something like this image 01

  • and make a void keyPressed, to every time i click on a letter the all img goes random, a do new stuff

  • Answer ✓

    ok

    this is 3D?

  • Answer ✓

    https://www.processing.org/examples/texturecube.html

    you need a image file and name it berlin-1.jpg

  • Im not sure...I was searching for some geometric images, and seeing the deep of the image, maybe.

  • Answer ✓

    and change P3D to OPENGL

  • i'm getting an error because a library

  • PImage tex=null;
    float rotx = PI/4;
    float roty = PI/4;
    
    void setup() {
      size(640, 360, OPENGL);
      tex = loadImage("berlin-1.jpg");
      if (tex==null) 
        println("image -> null !!!!!!!!!"); 
      textureMode(NORMAL);
      fill(255);
      stroke(color(44, 48, 32));
      initImage() ;
    }
    
    void draw() {
      // empty
    }
    
    void keyPressed() {
      initImage();
    }
    
    void initImage() {
      background(0);
      int max = int(random(179)); 
      for (int i=0; i < max; i++) {
        pushMatrix(); 
        noStroke();
        mouseSimulator();
        translate(random(-width*1, width*1), random(-height*1, height*1), -100*(i));
        rotateX(rotx);
        rotateY(roty);
        scale(90);
        TexturedCube(tex);
        popMatrix();
      }
    }
    
    void mouseSimulator() {
      float rate = 1.01;
      rotx = (random(2*PI)) * rate;
      roty = (random(2*PI)) * rate;
    }
    
    void TexturedCube(PImage tex) {
      beginShape(QUADS);
      texture(tex);
    
      // Given one texture and six faces, we can easily set up the uv coordinates
      // such that four of the faces tile "perfectly" along either u or v, but the other
      // two faces cannot be so aligned.  This code tiles "along" u, "around" the X/Z faces
      // and fudges the Y faces - the Y faces are arbitrarily aligned such that a
      // rotation along the X axis will put the "top" of either texture at the "top"
      // of the screen, but is not otherwised aligned with the X/Z faces. (This
      // just affects what type of symmetry is required if you need seamless
      // tiling all the way around the cube)
    
      // +Z "front" face
      vertex(-1, -1, 1, 0, 0);
      vertex( 1, -1, 1, 1, 0);
      vertex( 1, 1, 1, 1, 1);
      vertex(-1, 1, 1, 0, 1);
    
      // -Z "back" face
      vertex( 1, -1, -1, 0, 0);
      vertex(-1, -1, -1, 1, 0);
      vertex(-1, 1, -1, 1, 1);
      vertex( 1, 1, -1, 0, 1);
    
      // +Y "bottom" face
      vertex(-1, 1, 1, 0, 0);
      vertex( 1, 1, 1, 1, 0);
      vertex( 1, 1, -1, 1, 1);
      vertex(-1, 1, -1, 0, 1);
    
      // -Y "top" face
      vertex(-1, -1, -1, 0, 0);
      vertex( 1, -1, -1, 1, 0);
      vertex( 1, -1, 1, 1, 1);
      vertex(-1, -1, 1, 0, 1);
    
      // +X "right" face
      vertex( 1, -1, 1, 0, 0);
      vertex( 1, -1, -1, 1, 0);
      vertex( 1, 1, -1, 1, 1);
      vertex( 1, 1, 1, 0, 1);
    
      // -X "left" face
      vertex(-1, -1, -1, 0, 0);
      vertex(-1, -1, 1, 1, 0);
      vertex(-1, 1, 1, 1, 1);
      vertex(-1, 1, -1, 0, 1);
    
      endShape();
    }
    //
    
  • Answer ✓

    I have to leave now...

    bye....

  • Ok, thank you for all. Bye

Sign In or Register to comment.