gpt4 book ai didi

java - 用按钮控制透明度

转载 作者:行者123 更新时间:2023-12-02 05:14:43 24 4
gpt4 key购买 nike

我正在尝试创建一个显示原色的草图,然后您可以使用按钮或 slider 控制其透明度。我正在使用 CnttrolP5 库。问题是,每当我将透明度变量放入 color() 时,一切都会变得困惑,甚至颜色都不会出现。

查看代码:

 import controlP5.*;

ControlP5 cp5;

int transparency;
color c = color(0, 160, 25);
color red;
color green;
color blue;


void setup() {
size(400, 600);
gui();

red = color(255, 0, 0, transparency); // WHWENEVER I PUT TRASNPARENCY VALUE DOES NOT WORK
green = color(0, 255, 0, transparency);
blue = color(0, 0, 255, transparency);
noStroke();
smooth();

}
// create gui
void gui() {

cp5 = new ControlP5(this);

// create radio button


cp5.addRadioButton("radio")
.setPosition(40, 200)
.setItemWidth(20)
.setItemHeight(20)
.addItem("red", 0)
.addItem("green", 1)
.addItem("blue", 2)
.setColorLabel(color(255))
.activate(2)

;

cp5.addRadioButton("level")
.setPosition(100, 200)
.setItemWidth(20)
.setItemHeight(20)
.addItem("ONE", 0)
.addItem("TWO", 1)
.addItem("THREE", 2)
.addItem("FOUR", 3)
.addItem("FIVE", 4)
.addItem("SIX", 5)
.addItem("SEVEN", 6)
.addItem("EIGHT", 7)
.addItem("NINE", 8)
.addItem("TEN", 9)
.setColorLabel(color(255))
.activate(2)

;

//add slider bar


/* cp5.addSlider("level")
.setPosition(60,20)
.setSize(100,20)
.setRange(0,10)
.setValue(5)
.setNumberOfTickMarks(11)
.setSliderMode(Slider.FLEXIBLE)
;*/

}


void radio(int theC) {
switch (theC) {
case (0):
c = red;
break;
case (1):
c = green;
break;
case (2):
c = blue;
break;

}
}

void level(int theC) {
switch (theC) {
case (0):
transparency = 15;
break;
case (1):
transparency = 13;
break;
case (2):
transparency = 11;
break;
case (3):
transparency = 9;
break;
case (4):
transparency = 7;
break;
case (5):
transparency = 6;
break;
case (6):
transparency = 5;
break;
case (7):
transparency = 4;
break;
case (8):
transparency = 2;
break;
case (9):
transparency = 1;
break;

}
}

void draw() {
background(220);

fill(c); //fill with color

ellipse(200, 400, 100, 100);

}

最佳答案

您在setup()中初始化了颜色(红、绿、蓝)变量,并且从未更改它们。如果你想改变透明度,你需要进入draw(),并且变量transparency需要初始化为某个值。

此外,仅当用户单击“单选”按钮时才更改颜色,以改进这一点,您需要从“radio()”抽象颜色逻辑

void radio(int theC) {
mode = theC;
}

其中mode是存储颜色模式的整数变量。并更新了 draw() 函数:

void draw() {
background(220);

switch (mode) {
case (0):
c = color(255, 0, 0, transparency);
break;
case (1):
c = color(0, 255, 0, transparency);
break;
case (2):
c = color(0, 0, 255, transparency);
break;
}

fill(c); //fill with color
ellipse(200, 400, 100, 100);
}

正如 @rlinden 正确提到的,您还应该将 alpha 值更新为 0-255。

关于java - 用按钮控制透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27060788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com