捕获值栏

发布于 2025-01-30 22:32:31 字数 2206 浏览 2 评论 0原文

我正在研究我的大学项目,并遇到了一个问题。我正在创建一个在处理中的滑块,将价值更改为0-100。我想使用我创建的按钮来捕捉10(10,20,30 ...)的值。它的外观是:

“左箭头灰色按钮,一个带有白色手柄和灰色背景的滑块以及右箭头方形灰色按钮。滑块背景有9个黑色垂直条,几乎是滑块高度的一半,表示滑块对“ ”的勾选,

我很抱歉,如果我的代码看起来很混乱,我是初学者。 这是我的代码:

int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0; 
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0; 
float syOffset = 0.0;

void setup() {
  size(550,300);
}

void draw() {
  background(0);
  fill (100);
  rect (75, 25, 400, 50); //the slider track
  
  if(mousePressed) {
  if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
    {x=mouseX;}
    }
    
  float value = map(x, 75, 475, 0, 100);

  fill(255);
  rect (x, 20, 3, 60); //the slider thumb, positioned at x}
  fill (100);

  if(mousePressed){
  if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
      mouseY > by-boxSize && mouseY < by+boxSize){
      if(x <=75){
    x=76;}
    fill(255);
      if(x >=475){
      x= x-40;
      }else if (x >= 435){
      x = x-40;
      }  
      }
      }
  
  rect(bx, by, boxSize, boxSize);
  fill(10);
  triangle(30, 50, 50, 40, 50, 60);
  
  fill(100);
  
  if(mousePressed){
  if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
      mouseY > sby-sboxSize && mouseY < sby+sboxSize){
        
  if(x >= 475){
    x=474;}      
        
    fill(255);
    x++;
      }
  }
  
  
  rect(483,25,50,50);
  fill(10);
  triangle(520, 50, 500, 40, 500, 60);
  
  println(value);
  

  line (115, 60, 115, 90);
  line (155, 60, 155, 90);
  line (195, 60, 195, 90);
  line (235, 60, 235, 90);
  line (275, 60, 275, 90);
  line (315, 60, 315, 90);
  line (355, 60, 355, 90);
  line (395, 60, 395, 90);
  line (435, 60, 435, 90);
}

我开始尝试弄清楚如何使其从100到90到80,依此类推。但是我遇到了一个问题,如果我按下按钮,它将达到80,而不是首先停止到90。如果(x&gt; = 435){x = x-40; 语句,我假设这是由于我的else el el el 。但是我似乎无法找到解决此问题的方法。

I am working on my university project and came across an issue. I am creating a slider in Processing that changes value to 0-100. And I want to it to snap into values of 10 (10,20,30...) using the button I created. Here's how it looks:

a left arrow square gray button, a slider with a white handle and gray background and a right arrow square gray button on a black background. the slider background has 9 black vertical bars, almost half the height of the slider which represent slider ticks to snap to

I am sorry if my code looks messy, I am a beginner.
Here is my code:

int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0; 
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0; 
float syOffset = 0.0;

void setup() {
  size(550,300);
}

void draw() {
  background(0);
  fill (100);
  rect (75, 25, 400, 50); //the slider track
  
  if(mousePressed) {
  if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
    {x=mouseX;}
    }
    
  float value = map(x, 75, 475, 0, 100);

  fill(255);
  rect (x, 20, 3, 60); //the slider thumb, positioned at x}
  fill (100);

  if(mousePressed){
  if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
      mouseY > by-boxSize && mouseY < by+boxSize){
      if(x <=75){
    x=76;}
    fill(255);
      if(x >=475){
      x= x-40;
      }else if (x >= 435){
      x = x-40;
      }  
      }
      }
  
  rect(bx, by, boxSize, boxSize);
  fill(10);
  triangle(30, 50, 50, 40, 50, 60);
  
  fill(100);
  
  if(mousePressed){
  if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
      mouseY > sby-sboxSize && mouseY < sby+sboxSize){
        
  if(x >= 475){
    x=474;}      
        
    fill(255);
    x++;
      }
  }
  
  
  rect(483,25,50,50);
  fill(10);
  triangle(520, 50, 500, 40, 500, 60);
  
  println(value);
  

  line (115, 60, 115, 90);
  line (155, 60, 155, 90);
  line (195, 60, 195, 90);
  line (235, 60, 235, 90);
  line (275, 60, 275, 90);
  line (315, 60, 315, 90);
  line (355, 60, 355, 90);
  line (395, 60, 395, 90);
  line (435, 60, 435, 90);
}

I started trying to figure out how to make it go from 100 to 90 to 80 and so on. But I came across a problem wherein if I press the button, it goes to 80 instead of it stopping to 90 first. I am assuming this is cause by my else if (x >= 435){x = x-40; statement. But I can't seem to figure out a way to fix this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2025-02-06 22:32:31

使用 mousepressed() 并将滑块移至下一行按下按钮时:

void mousePressed() {
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
        mouseY > by-boxSize && mouseY < by+boxSize) {  
        if (x > 75) {
            x = ((x+39 - 75) / 40) * 40 + 75;
            x -= 40;
        }
    }
    if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
        mouseY > sby-sboxSize && mouseY < sby+sboxSize) {
        if (x < 475) {
            x = ((x - 75) / 40) * 40 + 75;
            x += 40;
        }
    }
}

完成示例:

”“

int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0; 
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0; 
float syOffset = 0.0;

void setup() {
  size(550,300);
}

void mousePressed() {
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
        mouseY > by-boxSize && mouseY < by+boxSize) {  
        if (x > 75) {
            x = ((x+39 - 75) / 40) * 40 + 75;
            x -= 40;
        }
    }
    if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
        mouseY > sby-sboxSize && mouseY < sby+sboxSize) {
        if (x < 475) {
            x = ((x - 75) / 40) * 40 + 75;
            x += 40;
        }
    }
}


void draw() {
  background(0);
  fill (100);
  rect (75, 25, 400, 50); //the slider track
  
  if(mousePressed) {
  if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
    {x=mouseX;}
    }
    
  float value = map(x, 75, 475, 0, 100);

  fill(255);
  rect (x, 20, 3, 60); //the slider thumb, positioned at x}
  fill (100);

  if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
      mouseY > by-boxSize && mouseY < by+boxSize) {
      fill(255);
  }
  
  rect(bx, by, boxSize, boxSize);
  fill(10);
  triangle(30, 50, 50, 40, 50, 60);
  
  fill(100);
  if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
      mouseY > sby-sboxSize && mouseY < sby+sboxSize){
      fill(255);
  }
  
  
  rect(483,25,50,50);
  fill(10);
  triangle(520, 50, 500, 40, 500, 60);
  
  println(value);
  

  line (115, 60, 115, 90);
  line (155, 60, 155, 90);
  line (195, 60, 195, 90);
  line (235, 60, 235, 90);
  line (275, 60, 275, 90);
  line (315, 60, 315, 90);
  line (355, 60, 355, 90);
  line (395, 60, 395, 90);
  line (435, 60, 435, 90);
}

Use the mousePressed() and move the slider to the next line when the button is pressed:

void mousePressed() {
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
        mouseY > by-boxSize && mouseY < by+boxSize) {  
        if (x > 75) {
            x = ((x+39 - 75) / 40) * 40 + 75;
            x -= 40;
        }
    }
    if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
        mouseY > sby-sboxSize && mouseY < sby+sboxSize) {
        if (x < 475) {
            x = ((x - 75) / 40) * 40 + 75;
            x += 40;
        }
    }
}

Complete example:

int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0; 
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0; 
float syOffset = 0.0;

void setup() {
  size(550,300);
}

void mousePressed() {
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
        mouseY > by-boxSize && mouseY < by+boxSize) {  
        if (x > 75) {
            x = ((x+39 - 75) / 40) * 40 + 75;
            x -= 40;
        }
    }
    if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
        mouseY > sby-sboxSize && mouseY < sby+sboxSize) {
        if (x < 475) {
            x = ((x - 75) / 40) * 40 + 75;
            x += 40;
        }
    }
}


void draw() {
  background(0);
  fill (100);
  rect (75, 25, 400, 50); //the slider track
  
  if(mousePressed) {
  if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
    {x=mouseX;}
    }
    
  float value = map(x, 75, 475, 0, 100);

  fill(255);
  rect (x, 20, 3, 60); //the slider thumb, positioned at x}
  fill (100);

  if (mouseX > bx-boxSize && mouseX < bx+boxSize && 
      mouseY > by-boxSize && mouseY < by+boxSize) {
      fill(255);
  }
  
  rect(bx, by, boxSize, boxSize);
  fill(10);
  triangle(30, 50, 50, 40, 50, 60);
  
  fill(100);
  if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize && 
      mouseY > sby-sboxSize && mouseY < sby+sboxSize){
      fill(255);
  }
  
  
  rect(483,25,50,50);
  fill(10);
  triangle(520, 50, 500, 40, 500, 60);
  
  println(value);
  

  line (115, 60, 115, 90);
  line (155, 60, 155, 90);
  line (195, 60, 195, 90);
  line (235, 60, 235, 90);
  line (275, 60, 275, 90);
  line (315, 60, 315, 90);
  line (355, 60, 355, 90);
  line (395, 60, 395, 90);
  line (435, 60, 435, 90);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文