Two problems in code (table sheet)

Table table; 
int numRows = 30; // Number of rows
int numCols = 7; // Number of columns
float cellWidth = 100; // Adjustable cell width
float cellHeight = 27; // Adjustable cell height
float tableWidth, tableHeight;
float cellWidthColumn7 = 200; // Adjusted width for column 7
float tableX = 150; 
float tableY = 50; 

PFont arialBold;
String[][] cellTexts;
boolean isEditing = false;
int editingRow = -1, editingCol = -1;
String currentInput = "";

// Variables for movable equation
float equationYOffset = 0; // Vertical offset for the equation
float equationStep = 5; // Step size for moving the equation up and down

void setup() {
  size(1800, 980);
  surface.setResizable(true);
  arialBold = createFont("Arial-BoldMT", 12);
  table = new Table();
  table.addColumn("A"); // First column heading
  table.addColumn("B (mm)");   // Second column heading
  table.addColumn("C (mm)");   // Third column heading
  table.addColumn("D(mm)");    // Fourth column heading
  table.addColumn("E (mm)"); // Fifth column heading
  table.addColumn("F"); // Sixth column heading
  for (int i = 6; i < numCols; i++) {
    table.addColumn("Column " + (i + 1));
  }

  cellTexts = new String[numRows][numCols];
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols; j++) {
      cellTexts[i][j] = i == 0 ? getHeaderText(j) : "";
    }
  }

  tableWidth = (numCols - 1) * cellWidth + cellWidthColumn7;
  tableHeight = numRows * cellHeight;
}

void drawColumn7Cells() {
  float x = 6 * cellWidth + tableX;
  float y = tableY;

  // Draw the heading for column 7
  fill(173, 216, 230); // Light blue background for heading
  stroke(0);
  rect(x, y, cellWidthColumn7, cellHeight); // Draw heading cell
  fill(27, 27, 27); // Dark text color
  String headingText = getHeaderText(6); // Get heading for column 7
  float headingTextWidth = textWidth(headingText);
  float headingTextX = x + (cellWidthColumn7 - headingTextWidth) / 2;
  float headingTextY = y + cellHeight / 2 + 6;
  text(headingText, headingTextX, headingTextY);

  // Start drawing from the first row after the heading
  float startY = 1;
  float numRowsInBox = 29;
  y = startY * cellHeight + tableY; // Adjust y position for the first row
  float height = numRowsInBox * cellHeight;

  fill(255);
  stroke(0);
  strokeWeight(1);
  rect(x, y, cellWidthColumn7, height); // Draw the white box

  // Equation text
  String equationText = "Equation = D+E+F+G+H+I";
  textFont(arialBold);
  fill(0);
  textSize(12);
  float textWidth = textWidth(equationText);
  float textX = x + (cellWidthColumn7 - textWidth) / 2;
  float textY = y + equationYOffset + 360; // Adjust for equation offset
  text(equationText, textX, textY);

  // Constrain the equationYOffset to keep the equation within the box
  equationYOffset = constrain(equationYOffset, -height + cellHeight, 0);
}


void draw() {
  background(200);
  for (int i = 0; i < numRows; i++) {
    for (int j = 0; j < numCols - 1; j++) {
      float x = j * cellWidth + tableX;
      float y = i * cellHeight + tableY;
      fill(i == 0 ? 173 : 255, i == 0 ? 216 : 255, i == 0 ? 230 : 255);
      stroke(0);
      rect(x, y, cellWidth, cellHeight);

      String cellText = (isEditing && i == editingRow && j == editingCol) ? currentInput : cellTexts[i][j];
      textFont(i == 0 ? arialBold : createFont("Arial", 12));
      fill(27, 27, 27);
      textSize(12);
      float textWidth = textWidth(cellText);
      if (textWidth > cellWidth) {
        cellText = truncateText(cellText, cellWidth);
        textWidth = textWidth(cellText);
      }
      float textX = x + (cellWidth - textWidth) / 2;
      float textY = y + cellHeight / 2 + 6;
      text(cellText, textX, textY);
    }
  }

  drawColumn7Cells();
  noFill();
  rect(tableX, tableY, tableWidth, tableHeight);
}

void keyPressed() {
  if (keyCode == UP) {
    equationYOffset -= equationStep; // Move the equation up
  } else if (keyCode == DOWN) {
    equationYOffset += equationStep; // Move the equation down
  }
  // Constrain the offset to keep the equation within the white box
  equationYOffset = constrain(equationYOffset, 0, (29 - 1) * cellHeight - 30); 
}

String truncateText(String text, float maxWidth) {
  float width = textWidth(text);
  while (width > maxWidth && text.length() > 0) {
    text = text.substring(0, text.length() - 1);
    width = textWidth(text + "...");
  }
  return text + "...";
}

String getHeaderText(int columnIndex) {
  switch (columnIndex) {
    case 0: return "A";
    case 1: return "B";
    case 2: return "C";
    case 3: return "D";
    case 4: return "E";
    case 5: return "F";
    default: return "Column " + (columnIndex + 1);
  }
}

void mouseClicked() {
  int row = (mouseY - int(tableY)) / int(cellHeight);
  int col = (mouseX - int(tableX)) / int(cellWidth);
  if (row >= 0 && row < numRows && col >= 0 && col < numCols) {
    if (isEditing) {
      cellTexts[editingRow][editingCol] = currentInput;
    }
    isEditing = true;
    editingRow = row;
    editingCol = col;
    currentInput = cellTexts[row][col];
  }
}

void keyTyped() {
  if (isEditing) {
    if (key == '\n' || key == RETURN) {
      cellTexts[editingRow][editingCol] = currentInput;
      isEditing = false;
    } else if (key == BACKSPACE && currentInput.length() > 0) {
      currentInput = currentInput.substring(0, currentInput.length() - 1);
    } else if (key >= 32 && key <= 126 && currentInput.length() < 7) {
      currentInput += key;
    }
  }
}


i want to make prgrame in such away that user enter all digits in ever row from column 1 to 6 , than transfer these values to column 7 and perform mathemtiical equation and stored the result from column 7 to another column 8 in every row corresponding

1 Like