Search code examples
javafunctionprocessingdraw

Calling Function when i click the button on processing


I so Im making this graphing application. Its with an application called processing. Im pretty sure it uses java but it might be slightly different then java. Anyways So when you open the application (i think you need to have processing downloaded and open it from the right folder).

When you open it you see my graph with no data points and three buttons on each side. I have my buttons all linked to the functions and in the console they are showing they are working when I click them. But i just cant figure out how to display the graphs(the functions i made) with all the graph points and title etc when I click the button. Can anyone help thanks

drawHealthGraph(); rawPopulationGraph(); ```

they all work and if I put them all in the draw function they all show
up Its just I need to click it and then when I click the next one the
first one should go away and the second one should come up 

    String name;
    String title;

    void setup()
    {
      size(900, 600);
      loadTable1();   //data of individual countries
      noLoop();
      background(20);
      fill (200);
      textAlign(CENTER);
      textSize(24);
    
    //region labels
      countryNames("Asia", 050, 550);
      countryNames("E. Europe", 150, 550);
      countryNames("N. Africa", 250, 550);
      countryNames("LAmericaCarib", 350, 550);
      countryNames("Oceania", 450, 550);
      countryNames("Baltics", 550, 550);
      countryNames("E. Asia", 650, 550);
      countryNames("Sub sahara", 750, 550);
      countryNames("W. Euope", 850, 550);
    
    
    //buttons
      textSize(14);
      fill(245, 245, 245);
    
      rect(750, 100, 150, 30, 18);
      fill(0, 0, 0);
      text("GDP Graph", 820, 120);
      
    
      fill(245, 245, 245);
      rect(750, 140, 150, 30, 18);
      fill(0, 0, 0);
      text("Health Graph", 820, 160);
    
      fill(245, 245, 245);
      rect(750, 180, 150, 30, 18);
      fill(0, 0, 0);
      text("Population Graph", 820, 200);
    }
    
    void draw()
    {
    
    
          
    
    }
    
    //butttons clicked activate otger graphs
    void mousePressed() {
      if (mouseX>750 && mouseX<900 && mouseY>100 &&mouseY<130) {
        title("GDP Graph", 450, 100);
        drawIncomeGraph();
      }
    
      if (mouseX>750 && mouseX<900 && mouseY>140 &&mouseY<170) {
        fill (255);
        title("Health Graph", 450, 100);
        drawHealthGraph();
      }
      if (mouseX>750 && mouseX<900 && mouseY>180 &&mouseY<210) {
        background (255);
        textSize(14);
    
        title("Population Graph", 450, 100);
        drawPopulationGraph();
      }
    }
Table table1;
String[] c;
String[] r;
float[] income;
float[] health;
float[] population;
int N;

//parse data
void loadTable1()
{
  table1 = loadTable("health-income.csv", "header");

  N = table1.getRowCount();
  c = new String[N];
  r = new String[N];
  income = new float[N];
  health = new float[N];
  population = new float[N];

  N = table1.getRowCount();
  for (int i=0; i<N; i++)
  {
    TableRow row = table1.getRow(i);
    c[i] = row.getString("country");
    r[i] = row.getString("region");
    income[i] = row.getFloat("income");
    health[i] = row.getFloat("health");
    population[i] = row.getFloat("population");
  }
}


float newIncome;
float averageIncome;
float g;

//calculates income graph
void incomeSorter(String region) {
  for (int i=0; i<N; i++) {
    if (r[i].equals(region)) {
      newIncome = (income[i] + newIncome);
      g++;
    }
    averageIncome=(newIncome/3500);
  }
  println(averageIncome);
}

//draws income graph
void drawIncomeGraph() {
  textAlign(CENTER);

  fill(255, 255, 255);
  title("GDP Graph", 450, 100);


  incomeSorter("ASIA (EX. NEAR EAST)         ");
  drawDots(50, 600-averageIncome, 15, 15);


  incomeSorter("EASTERN EUROPE                     ");
  drawDots(150, 600-averageIncome, 15, 15);


  incomeSorter("NORTHERN AFRICA                    ");
  drawDots(250, 600-averageIncome, 15, 15);


  incomeSorter("LATIN AMER. & CARIB    ");
  drawDots(350, 600-averageIncome, 15, 15);


  incomeSorter("OCEANIA                            ");
  drawDots(450, 600-averageIncome, 15, 15);


  incomeSorter("BALTICS                            ");
  drawDots(550, 600-averageIncome, 15, 15);


  incomeSorter("NEAR EAST                          ");
  drawDots(650, 600-averageIncome, 15, 15);


  incomeSorter("SUB-SAHARAN AFRICA                 ");
  drawDots(750, 650-averageIncome, 15, 15);


  incomeSorter("WESTERN EUROPE                     ");
  textSize(14);

  fill(143, 182, 171);
  rect(750, 100, 150, 30, 18);
  fill(0, 0, 0);
  text("GDP Graph", 820, 120);
}

//calculaties health graph
///health graphing sorter data
float newHealth;
float averageHealth;
float h;

void healthSorter(String region) {
  for (int i=0; i<N; i++) {
    if (r[i].equals(region)) {
      newHealth = (health[i] + newHealth);
      h++;
    }
    averageHealth=(newHealth/h*5);
  }
  println("hi" + averageHealth);
}

//draws population graph
void drawHealthGraph() {
  
  textAlign(CENTER);

  fill(204, 204, 204);
  title("Health Graph", 450, 100);

  healthSorter("ASIA (EX. NEAR EAST)         ");
  drawDots(50, averageHealth, 15, 15);

  healthSorter("EASTERN EUROPE                     ");
  drawDots(150, averageHealth, 15, 15);


  healthSorter("NORTHERN AFRICA                    ");
  drawDots(250, averageHealth, 15, 15);


  healthSorter("LATIN AMER. & CARIB    ");
  drawDots(350, averageHealth, 15, 15);


  healthSorter("OCEANIA                            ");
  drawDots(450, averageHealth, 15, 15);


  healthSorter("BALTICS                            ");
  drawDots(550, averageHealth, 15, 15);


  healthSorter("NEAR EAST                          ");
  drawDots(650, averageHealth, 15, 15);


  healthSorter("SUB-SAHARAN AFRICA                 ");
  drawDots(750, averageHealth, 15, 15);


  healthSorter("WESTERN EUROPE                     ");
  drawDots(850, averageHealth, 15, 15);
  textSize(14);
  fill(143, 182, 171);
  rect(750, 140, 150, 30, 18);
  fill(0, 0, 0);
  text("Health Graph", 820, 160);
}

float newPopulation;
float averagePopulation;
float f;

//calculates population graph
void populationSorter(String region) {
  for (int i=0; i<N; i++) {
    if (r[i].equals(region)) {
      newPopulation = (population[i] + newPopulation);
      f++;
    }
    averagePopulation = (newPopulation/f/165000);
  }
  println("hey" + averagePopulation);
}
//draws population graph
void drawPopulationGraph() {
  textAlign(CENTER);

  title("Population Graph", 450, 100);

  populationSorter("ASIA (EX. NEAR EAST)         ");
  drawDots(50, averagePopulation-1000, 15, 15);


  populationSorter("EASTERN EUROPE                     ");
  drawDots(150, 800-averagePopulation, 15, 15);


  populationSorter("NORTHERN AFRICA                    ");
  drawDots(250, 800-averagePopulation, 15, 15);


  populationSorter("LATIN AMER. & CARIB    ");
  drawDots(350, 650-averagePopulation, 15, 15);


  populationSorter("OCEANIA                            ");
  drawDots(450, 650-averagePopulation, 15, 15);


  populationSorter("BALTICS                            ");
  drawDots(550, 750-averagePopulation, 15, 15);


  populationSorter("NEAR EAST                          ");
  drawDots(650, 750-averagePopulation, 15, 15);


  populationSorter("SUB-SAHARAN AFRICA                 ");
  drawDots(750, 750-averagePopulation, 15, 15);


  populationSorter("WESTERN EUROPE                     ");
  drawDots(850, 750-averagePopulation, 15, 15);

  fill(143, 182, 171);
  rect(750, 180, 150, 30, 18);
  fill(0, 0, 0);
  textSize(14);

  text("Population Graph", 820, 200);
}

//Function for the name headings
int w;
int v;
void countryNames(String name, int w, int v) {
  textSize(14);
  text(name, w, v);
}
int startX;
int whatData;
int sizeCircle;
int sizeCircle2;

void drawDots (int startX, float whatData, int sizeCircle, int sizeCircle2) {
  fill(204, 204, 204);
  ellipse(startX, whatData, sizeCircle, sizeCircle2);
}


void title(String title, int r, int q) {
  textSize(24);
  text(title, r, q);
}

Solution

  • If you want to draw a new graph with the push of each button you would first of all need to remove the noLoop(); from setup(). The draw() loop is needed to change the different graphs. To remove the previous graph you need to 'erase' it by redrawing the background and all the controls. Code such as the following should help you achieve the desired effect:

    void loadLabelsAndButtons() {
      background(20);
      fill (200);
      textAlign(CENTER);
      textSize(24);
      
      //region labels
      countryNames("Asia", 050, 550);
      countryNames("E. Europe", 150, 550);
      countryNames("N. Africa", 250, 550);
      countryNames("LAmericaCarib", 350, 550);
      countryNames("Oceania", 450, 550);
      countryNames("Baltics", 550, 550);
      countryNames("E. Asia", 650, 550);
      countryNames("Sub sahara", 750, 550);
      countryNames("W. Euope", 850, 550);
    
      //buttons
      textSize(14);
      fill(245, 245, 245);
    
      rect(750, 100, 150, 30, 18);
      fill(0, 0, 0);
      text("GDP Graph", 820, 120);
    
      fill(245, 245, 245);
      rect(750, 140, 150, 30, 18);
      fill(0, 0, 0);
      text("Health Graph", 820, 160);
    
      fill(245, 245, 245);
      rect(750, 180, 150, 30, 18);
      fill(0, 0, 0);
      text("Population Graph", 820, 200);
    }
    
    void setup() {
      size(900, 600);
      loadTable1();   //data of individual countries
      loadLabelsAndButtons();
    }
    
    void draw() {
    }
    
    //buttons clicked activate other graphs
    void mousePressed() {
      if (mouseX>750 && mouseX<900 && mouseY>100 &&mouseY<130) {
        loadLabelsAndButtons();
        title("GDP Graph", 450, 100);
        drawIncomeGraph();
      }
    
      if (mouseX>750 && mouseX<900 && mouseY>140 &&mouseY<170) {
        loadLabelsAndButtons();
        title("Health Graph", 450, 100);
        drawHealthGraph();
      }
    
      if (mouseX>750 && mouseX<900 && mouseY>180 &&mouseY<210) {
        loadLabelsAndButtons();
        title("Population Graph", 450, 100);
        drawPopulationGraph();
      }
    }