用箭头按钮创建滑块
如何从侧面创建一个带有两个箭头按钮的滑块?单击时,箭头/三角形应变白。每次单击箭头按钮时,滑块都应移动。
这是指向它应该外观的链接
所做的
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);
fill (200);
rect (10, 25, 50, 50);
{
if (mousePressed == true) {
fill(255);
} else {
fill(0);
}
triangle (50, 60, 50, 40, 15, 50);
}
fill (200);
rect (490, 25, 50, 50);
{
if (mousePressed == true) {
fill(255);
} else {
fill(0);
}
triangle (500, 60, 500, 40, 535, 50);
}
println(x);
}
,这是我到目前为止 我单击屏幕上的任何地方,我的问题是两个箭头变白。我需要它可以单独运行。每次我单击箭头按钮时,滑块都不会移动
How can I create a slider with two arrow buttons from the side? The arrows/triangle should turn white when clicked. And the slider should move each time the arrow button is clicked.
here's the link to what it should look like
and here's what I've done so far
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);
fill (200);
rect (10, 25, 50, 50);
{
if (mousePressed == true) {
fill(255);
} else {
fill(0);
}
triangle (50, 60, 50, 40, 15, 50);
}
fill (200);
rect (490, 25, 50, 50);
{
if (mousePressed == true) {
fill(255);
} else {
fill(0);
}
triangle (500, 60, 500, 40, 535, 50);
}
println(x);
}
When I click anywhere on the screen, my problem is that both arrows turn white. I need it to individually function. And the slider is not moving every time I click the arrow buttons
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决此问题的以下方法使用了4个不同的类:forwardarrow,backarrow,slider和valuefield。箭头填充颜色由其各自类的Press()方法控制。
您的代码进行修订;
The following approach to this problem uses 4 different classes: ForwardArrow, BackArrow, Slider, and ValueField. Arrow fill color is controlled by the press() method of its respective class.
A revision of your code follows;
您有部分滑块/跟踪栏的逻辑权利,因此仅在范围内更改
x
。这仅在此阶段水平发生,但是您也可以使用相同的逻辑来检查水平限制。同样,您可以检查光标是否在任何矩形的边界内(无论是滑块还是一个按钮):如果您可以采用该逻辑,而不是复制/粘贴不同的x,y,那不是很好,宽度,相同4个语句的高度参数您可以将功能分组为可重复使用的代码块?
那是什么功能。您已经已经使用了它们(定义
setup()
/draw()
,呼叫背景()
/fill()
/等已经提供了
布尔值重叠(int x,int y,int width,int height)
函数,非常适合您尝试实现:传递x,y 获取布尔值。
,宽度,高度或一个按钮,并使用
repret Rect()
You've got part of the logic right for the slider/trackbar so it changes
x
only within a range. This happens horizontally only at this stage, but you can use the same logic to check horizontal limits as well. Similarly, you can check if the cursor is within the bounds of any rectangle (be it the slider or either of the buttons):Wouldn't it be nice if you could take that logic and instead of copy/pasting the different x,y,width,height parameters for the same 4 statements you could group that functionality in a reusable block of code ?
That what functions are for. You're already using them already (defining
setup()
/draw()
, callingbackground()
/fill()
/etc.The Processing Button example already provides the
boolean overRect(int x, int y, int width, int height)
function which is perfect for you're trying to achieve: pass in thex,y,width,height
or a button and get back boolean value.Here's your code using the
overRect()
: