英文:
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)<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;
}
}
答案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)<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;
}
}
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)<60){
cursor(HAND);
} else {
cursor(ARROW);
}
if (drawText) {
fill(#FFFF00);
text("Hi!", 110, 110);
}
}
void mousePressed(MouseEvent evt) {
if (evt.getCount() == 2) { // detect double click
if (mouseButton == LEFT && dist(mouseX,mouseY,110,110)<60) {
drawText = !drawText;
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论