用户按钮处理

时间:2014-02-28 10:52:59

标签: java user-interface processing

我对编程比较陌生,我对VBA和matlab有非常基本的了解,但对于我的uni设计项目,我需要生成一个GUI,由于某些原因我必须在处理过程中。我试图编辑示例按钮以创建两个按钮,当您将鼠标悬停在它们上方时会改变颜色,然后在单击时更改背景颜色,但它不会执行任何操作。 脚本如下:

PFont f;
int playX, playY; 
int stopX, stopY;
int buttonsize = 90;
color playColor, stopColor;
color playHighlight, stopHighlight;
boolean playOver = false;
boolean stopOver = false;
int n, i;

void setup() {
  size(800,500);
  f = createFont("Arial",16,true); // STEP 3 Create Font
  playColor = color(0);
  playHighlight = color(200);
  stopColor = color(0);
  stopHighlight = color(200);
  playX = (width/4)-(buttonsize/2);
  playY = 7*(height/10);
  stopX = (3*(width/4))-(buttonsize/2);
  stopY = 7*(height/10);
  n = 0;
  i = 0;
 }

void draw() {
  background(250);
  textFont(f,16);                 // STEP 4 Specify font to be used
  fill(0);                        // STEP 5 Specify font color 
  text("Set BPM:",width/10,2*(height/10));
  text("Choose File:",width/10,height/2);

    if (playOver) {
    fill(playHighlight);
  } else {
    fill(playColor);
  }
  stroke(0);
  rect(playX, playY, buttonsize, buttonsize);

  if (stopOver) {
    fill(stopHighlight);
  } else {
    fill(stopColor);
  }
  stroke(0);
  rect(stopX, stopY, buttonsize, buttonsize);
}

void update(int x, int y) {
  if (overplay(playX, playY, buttonsize, buttonsize)) {
    playOver = true;
    stopOver = false;
  } else if (overstop(stopX, stopY, buttonsize, buttonsize)) {
    stopOver = true;
    playOver = false;
   } else {
    stopOver = playOver = false;
  }
}

void mousePressed() {

  if (playOver) {
    if (i == 0) {
      i = 1;
      playColor = color(255,0,0);
    } else {
      i = 0;
      playColor = color(0);
    }
    }


   if (stopOver) {
    if (n == 0) {
      n = 1;
      stopColor = color(255,0,0);
    } else {
      n = 0;
      stopColor = color(0);
    }
  } 
 }



boolean overplay(int x, int y, int width, int height)  {
  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

boolean overstop(int x, int y,int width, int height) {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
     return true;
  } else {
    return false;
  }
}

1 个答案:

答案 0 :(得分:0)

代码有点困惑。首先,你永远不会叫update(),这是基本的缺陷。在update(0,0)内添加draw(),事情就会开始起作用。)。

您可以在update()调用中使用任意数字而不是0,因为这些数字从未使用过,如果它们更好,则可以更好地删除它们。如果您需要将每个按钮的参数传递给它们,还有overStop()overPlay()的重点是什么?我认为您应该制作一个唯一的isOver(x,y,xw,yw)并使用每个按钮参数调用它,或者使用不带参数的isOverStop() ...

这里更好的做法是制作一个Button类。这将使事情更加清晰,易于维护和编写。

另一个注意事项,您使用in的方式可能更容易使其成为布尔值并使用符号:i = !i来表示其状态。

编辑,示例代码:

Button play;


void setup() {
  size(300, 300);
  background(255);
  play = new Button(50, 50, 30, 30);
}

void draw() {
  background(255);

  //display it...
  play.display();

  // using the button...
  if (play.isOn()) {
    fill(200, 30, 30);
    textSize(40);
    text("I'm playing!", 50, 200);
  }
}


void mouseClicked() {
  //listen for clicks
  play.update();
}



class Button {
  //coordinates
  int x, y, w, h;

  // state
  boolean on = false;

  //colors
  color[] colors = new color[4];

  Button(int _x, int _y, int _w, int _h) {

    //init everything
    x = _x;
    y = _y;
    w = _w;
    h = _h;

    //create some colors
    colors[0] = color(180); //off regular
    colors[1] = color(100); // off over
    colors[2] = color(180, 50, 180); // on
    colors[3] = color(100, 50, 100); // on over
  }


  void display() {
    // decide wich color to use
    fill(colorEngine());

    noStroke();

    //draw
    rect(x, y, w, h);
  }

  void update() {
    // toogle state
    if (isOver())
      on = !on;
  }

boolean isOn(){
  return on;
}

  color colorEngine() {
    if (!on) { // if off...
      if (!isOver()) { //and not over
        return colors[0];
      }
      else {//if off and over
        return colors[1];
      }
    }
    else {// if on...
      if (!isOver()) { //... and not over
        return colors[2];
      }
      else {// on and over
        return colors[3];
      }
    }
  }


  boolean isOver() {
    return mouseX >= x && mouseX <= x+w &&
      mouseY >= y && mouseY <= y+h;
  }
}//