Programming efficiency

I am working on making a number pad for what will eventually turn into an alarm clock. I have a bit of code that draws the numbers 1-9 and 0 and also draws boxes around each number. I am trying to make the code scale so that it looks nice no matter what the screen resolution is. I was just wondering if someone knew of a way to make this code look a little bit better. One simple fix I think I am going to do is have variables to represent (height/15)/2, 3*(height/15)/2, and (height/15). but that will still only shorten the code so much. Does anyone know some good reading material that relates to making programs scale well?

int r = 0;
  for(int i = 1; i < 10; i++){
    if(i%3 == 1){
      rect((x-((3*(height/15))/2))-(height/15)/2, (y-((3*(height/15))/2))+(r*(height/15))-(height/15)/3, height/15, height/15, 10);
      text(i, x-((3*(height/15))/2), (y-((3*(height/15))/2))+(r*(height/15)));
    }
    else if(i%3 == 2){
      rect(x-(height/15)/2-(height/15)/2, (y-((3*(height/15))/2))+(r*(height/15))-(height/15)/3, height/15, height/15, 10);
      text(i, x-(height/15)/2, (y-((3*(height/15))/2))+(r*(height/15)));
    }
    else if(i%3 == 0){
      rect(x+(height/15)/2-(height/15)/2, (y-((3*(height/15))/2))+(r*(height/15))-(height/15)/3, height/15, height/15, 10);
      text(i, x+(height/15)/2, (y-((3*(height/15))/2))+(r*(height/15)));
      r++;
    }
  }
  rect(x-(height/15)/2-(height/15)/2, (y-((3*(height/15))/2))+(r*(height/15))-(height/15)/3, height/15, height/15, 10);
  text("0", x-(height/15)/2, (y-((3*(height/15))/2))+(r*(height/15)));
Tagged:

Answers

  • Look at the tutorials

    There is one on objects

    With oop you can make a list of rects and draw them in a single spot

    Their data is stored and then they are shown in a central place

  • Thanks. I'll take a look.

  • this is what I meant basically

    a class Cell (which is a rect basically) and an array of that (here made as a grid)

    // demo 
    
    // Array of objects of the class 
    Cell[] grid; 
    
    int cols = 4; 
    int rows = 6;
    
    void setup() { 
      size(400, 400); 
      grid = new Cell[cols*rows];
      int k=0; 
      for (int i = 0; i < cols; i++) { 
        for (int j = 0; j < rows; j++) { 
          // Initialize each object
          grid[k] = new Cell(i*20+22, j*20+22, 20, 20, k);
          k++;
        }
      }
    }
    
    void draw() { 
      background(0); 
      for (int i = 0; i < cols*rows; i++) { 
        grid[i].display();
      }
    }
    
    // =============================================
    
    // A Cell class 
    class Cell { 
      float x, y; // x,y location
      float w, h; // width and height 
      String text="a";
    
      // Cell Constructor
      Cell(float tempX, float tempY, 
        float tempW, float tempH, 
        float tempTag) { 
        x = tempX; 
        y = tempY; 
        w = tempW; 
        h = tempH; // and tempTag ??? 
      }
    
      void display() { 
        stroke(255); 
        // Color 
        fill(0); 
        rect(x, y, w, h);
        fill(255);
        textAlign(CENTER, CENTER);
        text(text, 
          x+w/2, y+h/2);
      }
    }
    // 
    
Sign In or Register to comment.