controlP5:用于多个RadioButton结果的矩阵/ 2D数组

时间:2014-09-03 05:29:27

标签: radio-button processing control-p5

我试图查询用户从4组或更多组单选按钮中进行选择,其中每组中有5个按钮(处理2+)。我遇到问题的地方是通过从每组按钮中选择创建的数组并使其填充矩阵中的列,可以查询元素并打印2D数组并最终将其写为csv或tab txt文件。

import controlP5.*;
ControlP5 controlP5;

RadioButton c0;
RadioButton c1;
RadioButton c2;
RadioButton c3;

int cols = 5;
int rows = 4;
int[][] myArray = new int[cols][rows];

void setup() {
  size(600,650);
controlP5 = new ControlP5(this);
  c0 = controlP5.addRadioButton("ch0",60,60)
               .setSize(20,20)
               .setItemsPerRow(5)
               .setSpacingColumn(50)               
               .addItem("c03", 1)
               .addItem("c04", 2)
               .addItem("c05", 3)
               .addItem("c0AM", 4)
               .addItem("c0AF", 5)
               ;
  c1 = controlP5.addRadioButton("ch1",60,80)
               .setSize(20,20)
               .setItemsPerRow(5)
               .setSpacingColumn(50)
               .addItem("c13", 1)
               .addItem("c14", 2)
               .addItem("c15", 3)
               .addItem("c1AM", 4)
               .addItem("c1AF", 5)
               ;  
  c2 = controlP5.addRadioButton("ch2",60,100)
               .setSize(20,20)
               .setItemsPerRow(5)
               .setSpacingColumn(50)
               .addItem("c23", 1)
               .addItem("c24", 2)
               .addItem("c25", 3)
               .addItem("c2AM", 4)
               .addItem("c2AF", 5)

               ;  
  c3 = controlP5.addRadioButton("ch3",60,120)
               .setSize(20,20)
               .setItemsPerRow(5)
               .setSpacingColumn(50)        
               .addItem("c33", 1)
               .addItem("c34", 2)
               .addItem("c35", 3)
               .addItem("c3AM", 4)
               .addItem("c3AF", 5)

               ;
}

void draw() {
background(0);
}

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isGroup() && theEvent.name().equals("ch0") || theEvent.name().equals("ch0") ||       theEvent.name().equals("ch2") || theEvent.name().equals("ch3")){
   println(theEvent.name());
   println(theEvent.arrayValue());
   //float t=float(theEvent.arrayValue());
   //int[][] = { {float getGroup(),float[] getArrayValue()}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
//int cols = 4;
//int rows = 5;
//int[][] myArray = new int[cols][rows];

// Two nested loops allow us to visit every spot in a 2D array.   
// For every column I, visit every row J.
//for (int i = 0; i < cols; i++) {
// for (int j = 0; j < rows; j++) {
//myArray[i][j] = float(theEvent.arrayValue);
  }
} 

1 个答案:

答案 0 :(得分:1)

你把不同的东西混在一起。此外,您不需要检查整个数组并存储此信息。只需更新点击按钮。这是您的controlEvent

的新版本
void controlEvent(ControlEvent theEvent) {

  int cols = 4;
  int rows = 5;
  int[][] myArray = new int[cols][rows];      

  switch(theEvent.getId()){
    case 0:
      myArray[0][(int)theEvent.value()-1] = 1;
      break;
    case 1:
      myArray[1][(int)theEvent.value()-1] = 1;
      break;
    case 2:
      myArray[2][(int)theEvent.value()-1] = 1;
      break;
    case 3:
      myArray[3][(int)theEvent.value()-1] = 1;
      break;
  }  
  println("==== " + theEvent.getId() + " ===");
  println(myArray[theEvent.getId()]);
}

要执行此简单切换,您需要将ID参数添加到所有单选按钮,如下所示:

  c3 = controlP5.addRadioButton("ch3", 60, 120)
  .setId(3)
    .setSize(20, 20)
     ...

我不知道你究竟想要使用这个数组,所以我的实现使用它作为局部变量,所以每次调用这个事件时它都会被删除但是这可以通过将数组声明为全局变量然后删除只更新来避免列。