Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

A Problem with a piece of code for making a table

$
0
0

if i run the following program, the arrays x and y are sometimes empty and i dont know why.

P.S. the Input boxes are arranged from top down first and then from left to right.

PP.S. how can I change the formatting of this code to look like code

import org.gicentre.utils.stat.*;
import controlP5.*;
ControlP5 cp5;
Textfield[] a = new Textfield[80];
Button b;
XYChart lineChart;
float first_x = 50, first_y = 20;
boolean show_chart = false;
int lastnumber = 80;
float[] x, y;


void setup() {
  size(1200, 600);
  background(51);
  initialize();
  b = cp5.addButton("zeichnen").setPosition(first_y, 500+first_x).setSize(260, 20);
  lineChart = new XYChart(this);
}

void draw() {
  line(20, 247+first_x, 279, 247+first_x);
  if (show_chart) {
    lineChart.draw(first_x+260, first_y, width-30, height-30);
  }
}


//Button
void zeichnen() {
  //setting the arrays to the right length (!)
  for (int i = a.length-1; !(i<0); i--) {
    if (!"0".equals(a[i].getText())) {
      lastnumber = i;
    }
  }
  println(lastnumber);
  x = new float[lastnumber];
  y = new float[lastnumber];
  //copying the arrays
  for (int i = 0; i<y.length; i++) {
    x[i] = 10*i;
  }
  for ( int i = 0; i < x.length; i++ ) {
    y[i] = float(a[i].getText());
  }
  //(?)
  lineChart.setData(x, y);
  //formating
  lineChart.showXAxis(true);
  lineChart.showYAxis(true);
  lineChart.setMinY(0);
  lineChart.setYFormat("00");
  lineChart.setXFormat("000");
  lineChart.setPointColour(color(180, 50, 50, 100));
  lineChart.setPointSize(5);
  lineChart.setLineWidth(2);
  //(works)
  show_chart = true;
}



//make all Textboxes (works)
void initialize() {
  PFont font = createFont("arial", 16);
  cp5 = new ControlP5(this);
  for (int i = 0; i<a.length; i++) {
    a[i] = cp5.addTextfield(str(i)).setFont(font).setColor(color(255)).setColorBackground(0).setColorForeground(0).setText("0").setLabelVisible(false).setSize(50, 20);
    if (i<20) {
      a[i].setPosition(first_y, first_x+25*i);
    } else if (i<40) {
      a[i].setPosition(first_y+70, first_x+25*(i-20));
    } else if (i<60) {
      a[i].setPosition(first_y+140, first_x+25*(i-40));
    } else if (i<80) {
      a[i].setPosition(first_y+210, first_x+25*(i-60));
    } else {
      println("too much objects for in a[]");
    }
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles