java 单击处理按钮时调用函数

ubof19bj  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(207)

我正在制作这个图形应用程序。它有一个叫做处理的应用程序。我很确定它使用Java,但它可能会略有不同,然后Java。无论如何,所以当你打开应用程序(我认为你需要有处理下载,并从正确的文件夹打开它)。
当你打开它,你会看到我的图表没有数据点和三个按钮在每一方。我有我的按钮都链接到功能和控制台,他们显示他们正在工作时,我点击他们。但我只是不知道如何显示图表(我所做的功能)与所有的图表点和标题等,当我点击按钮。有人能帮忙吗,谢谢

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);
}
nwlls2ji

nwlls2ji1#

如果你想通过按下每个按钮来绘制一个新的图形,你首先需要移除noLoop();。draw()循环需要用来改变不同的图表。要删除之前的图表,你需要通过重绘背景和所有控件来“擦除”它。如下代码应该能帮助你达到预期效果:

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();
  }
}

相关问题