使用处理制作离散滑块

发布于 2025-01-30 12:41:46 字数 1780 浏览 3 评论 0原文

如何使用处理使我的滑块离散?这是我的代码。我需要将其与0-10的值进行离散。我已经提出了一些线指标,我只需要使其离散。您能给我一个关于如何做的想法吗?

到目前为止,我们现在所做的只是连续的滑块,拇指的行驶非常顺利。我们如何在同一滑块中添加步骤以制作离散的滑块?这是同一滑块的一个示例,但是有10个步骤,而不是返回之间的每个值。

    int x=75;
    
    void setup() {
      size(600,400);
    }
    
    void draw() {
      background(100);
      fill (200);
      rect (75, 25, 400, 50);
      stroke(0);
      
      if(mousePressed) {
      if (mouseX >75 && mouseX <= 475)
        {x=mouseX;}
        }
      fill(127,0,0);
      rect (x, 20, 9, 60); 
      fill (255);
 
      // Left Button
      fill (200);
      rect (10, 25, 50, 50);
    {
      if (mousePressed == true) {
        if (mouseX <= 50 && (mouseY >= 40 && mouseY <= 60)) {
        fill(255);
        if (x>100){
        x-=20;
      } else {
        x=75;
      }
      } else {
        fill(0);
      }
      }
      if (mousePressed == false) {
      fill (0);
      }
      triangle (50, 60, 50, 40, 15, 50);
    }
      
      // Right button
      fill (200);
      rect (490, 25, 50, 50);
      {
      if (mousePressed == true) {
        if (mouseX >= 500 && (mouseY >= 40 && mouseY <= 60)) {
        fill(255);
        if (x<470){
          x+=20;
      } 
      else {
        x=470;
      }
        } else {
        fill(0);
      }
 }
      if (mousePressed == false) {
      fill (0);
      }
      triangle (500, 60, 500, 40, 535, 50);
    }
      println(x);
      
      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);
    }

How can I make my slider discrete using Processing? Here is my code. I need to make it discrete with values from 0-10. I already put some line indicators, what I only need is to make it discrete. Can you give me an idea of how to do it?

So far, all we have made right now are continuous sliders, and the thumb travels very smoothly. How can we add steps to the same slider to make a discrete slider? Here is an example of the same slider but with 10 steps instead of returning every value in between.

    int x=75;
    
    void setup() {
      size(600,400);
    }
    
    void draw() {
      background(100);
      fill (200);
      rect (75, 25, 400, 50);
      stroke(0);
      
      if(mousePressed) {
      if (mouseX >75 && mouseX <= 475)
        {x=mouseX;}
        }
      fill(127,0,0);
      rect (x, 20, 9, 60); 
      fill (255);
 
      // Left Button
      fill (200);
      rect (10, 25, 50, 50);
    {
      if (mousePressed == true) {
        if (mouseX <= 50 && (mouseY >= 40 && mouseY <= 60)) {
        fill(255);
        if (x>100){
        x-=20;
      } else {
        x=75;
      }
      } else {
        fill(0);
      }
      }
      if (mousePressed == false) {
      fill (0);
      }
      triangle (50, 60, 50, 40, 15, 50);
    }
      
      // Right button
      fill (200);
      rect (490, 25, 50, 50);
      {
      if (mousePressed == true) {
        if (mouseX >= 500 && (mouseY >= 40 && mouseY <= 60)) {
        fill(255);
        if (x<470){
          x+=20;
      } 
      else {
        x=470;
      }
        } else {
        fill(0);
      }
 }
      if (mousePressed == false) {
      fill (0);
      }
      triangle (500, 60, 500, 40, 535, 50);
    }
      println(x);
      
      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);
    }

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

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

发布评论

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

评论(1

昔日梦未散 2025-02-06 12:41:46

解决您的问题的方法不同:

最简单的实现是将矩形绘制到正确的位置,但像您已经一样存储X值。

标签在滑块中的位置是:

int length = 400; //length of the slider
int steps = 10; //steps for the slider
int start = 75; //start of the slider

int currentStep = round(((float)steps)*(x - start)/length);
//The float parse is important
//round(), ceil() or floor() will work 

int correctedPos = round(((float)length)/steps*currentStep) + start;

然后只需在正确的位置绘制RECT即可;

rect(correctedPos, 20, 9, 60);

第二个选项将存储CurrentStep,而不是您已经存储的X。您的按钮将必须增加或减少CurrentStep而不是X。然后,您还必须按照我已经显示的,必须计算correctedPos

There are different approaches to your problem:

The easiest implementation would be drawing the rectangle to the correct position but storing the x value as you already do.

The position of your label in the slider is:

int length = 400; //length of the slider
int steps = 10; //steps for the slider
int start = 75; //start of the slider

int currentStep = round(((float)steps)*(x - start)/length);
//The float parse is important
//round(), ceil() or floor() will work 

int correctedPos = round(((float)length)/steps*currentStep) + start;

Then just draw the rect at the correct position;

rect(correctedPos, 20, 9, 60);

The second option would be storing the currentStep and not the x that you already store. Your buttons will have to increment or decrease currentStep instead of x. Then you will also have to calculate the correctedPos as I have already shown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文