将我的椭圆变成一个按钮,在同一个窗口上显示一些数据。

huangapple 未分类评论42阅读模式
英文:

Making my ellipse a button to show some data on the same window?

问题

boolean drawText = false;
void setup(){
  size(800,600);
}
void draw(){
  background(0);
  noStroke();
  fill(245, 27, 27, 151);
  ellipse(110,110,120,120);
  if(dist(mouseX,mouseY,110,110)<60){
    cursor(HAND);
  } else {
    cursor(ARROW);
  }
  if (drawText) {
    fill(#FFFF00);
    text("Hi!", 110, 110);
  }
}
void mousePressed() {
  if (mouseButton == LEFT && dist(mouseX,mouseY,110,110)<60) {
    drawText = true;
  } else if (mouseButton == LEFT && dist(mouseX,mouseY,110,110)<60){
   drawText = false; 
  }
}
英文:

Hello im trying to make my ellipse shape a button so that when i double click it, it should show a snap-view of the data. I've created some code but its not 100% functional and would be nice if someone can help me.

Code:

boolean drawText = false;
void setup(){
  size(800,600);
}
void draw(){
  background(0);
  noStroke();
  fill(245, 27, 27, 151);
  ellipse(110,110,120,120);
  if(dist(mouseX,mouseY,110,110)&lt;60){
    cursor(HAND);
  } else {
    cursor(ARROW);
  }
  if (drawText) {
    fill(#FFFF00);
    text(&quot;Hi!&quot;, 110, 110);
  }
}
void mousePressed() {
  if (mouseButton == LEFT &amp;&amp; dist(mouseX,mouseY,110,110)&lt;60) {
    drawText = true;
  } else if (mouseButton == LEFT &amp;&amp; dist(mouseX,mouseY,110,110)&lt;60){
   drawText = false; 
  }
}

答案1

得分: 0

我不确定是否正确理解了您的问题但根据您的问题和在其下的评论我理解您希望在单击按钮时显示文本再次单击按钮时隐藏文本如果是这样以下代码适用于您

boolean drawText = false;
void setup(){
  size(800,600);
}
void draw(){
  background(0);
  noStroke();
  fill(245, 27, 27, 151);
  ellipse(110,110,120,120);
  if(dist(mouseX,mouseY,110,110)<60){
    cursor(HAND);
  } else {
    cursor(ARROW);
  }
  if (drawText) {
    fill(#FFFF00);
    text("Hi!", 110, 110);
  }
}
void mousePressed() {
  if (mouseButton == LEFT && dist(mouseX,mouseY,110,110)<60) {
    drawText = !drawText;
  }
}

另一种理解您的问题的方式是,您希望仅在双击按钮后显示文本。在这种情况下,您可以在mousePressed中使用evt.getCount()函数。它在单击后返回1,在双击后返回2,依此类推。以下是一些使其工作的代码:

boolean drawText = false;
void setup(){
  size(800,600);
}
void draw(){
  background(0);
  noStroke();
  fill(245, 27, 27, 151);
  ellipse(110,110,120,120);
  if(dist(mouseX,mouseY,110,110)<60){
    cursor(HAND);
  } else {
    cursor(ARROW);
  }
  if (drawText) {
    fill(#FFFF00);
    text("Hi!", 110, 110);
  }
}
void mousePressed(MouseEvent evt) {
  if (evt.getCount() == 2) {  // 检测双击
    if (mouseButton == LEFT && dist(mouseX,mouseY,110,110)<60) {
      drawText = !drawText;
    }
  }
}
英文:

I am not sure whether I understand your question the right way, but judging from it and your comment under it, I understand, that you want the text to be shown, when the button is clicked once and hidden when the button is clicked again. If so, this code should work for you:

boolean drawText = false;
void setup(){
  size(800,600);
}
void draw(){
  background(0);
  noStroke();
  fill(245, 27, 27, 151);
  ellipse(110,110,120,120);
  if(dist(mouseX,mouseY,110,110)&lt;60){
    cursor(HAND);
  } else {
    cursor(ARROW);
  }
  if (drawText) {
    fill(#FFFF00);
    text(&quot;Hi!&quot;, 110, 110);
  }
}
void mousePressed() {
  if (mouseButton == LEFT &amp;&amp; dist(mouseX,mouseY,110,110)&lt;60) {
    drawText = !drawText;
  }
}

So, when the button is clicked now, drawText is changed to its opposite: If it was true before, then it is set to false and the other way round. That way, clicking the button makes the text visible and clicking it again makes the text invisible again.

Edit: Another way of understanding your question

Another way of understanding your question is that you want the text to be shown only after a double-click on the button. In that case, you can make use of the evt.getCount() function inside mousePressed. It resturns 1 after a single click, 2 after a double click and so on. Here is some code to make that work:

boolean drawText = false;
void setup(){
  size(800,600);
}
void draw(){
  background(0);
  noStroke();
  fill(245, 27, 27, 151);
  ellipse(110,110,120,120);
  if(dist(mouseX,mouseY,110,110)&lt;60){
    cursor(HAND);
  } else {
    cursor(ARROW);
  }
  if (drawText) {
    fill(#FFFF00);
    text(&quot;Hi!&quot;, 110, 110);
  }
}
void mousePressed(MouseEvent evt) {
  if (evt.getCount() == 2) {  // detect double click
    if (mouseButton == LEFT &amp;&amp; dist(mouseX,mouseY,110,110)&lt;60) {
      drawText = !drawText;
    }
  }
}

huangapple
  • 本文由 发表于 2020年4月5日 02:33:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/61032943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定