在一个项目中,我编写了一段代码来创建带有图标的计算机墙纸。我设置的一个图标,用于在单击时绘制加载条(无效mousePressed)。我希望能够看到矩形(加载栏)开始于一个确定的位置,使用RectMode(角点),并让宽度每隔几秒钟增加一次,直到栏填满大约3/4,然后停止并保持不变。
请给出建议
这将绘制完成的条形图,但我希望在几秒钟内看到每个增量
void setup(){
size(800,600);
}
void mousePressed(){
if (mousePressed && mouseX>width/4 && mouseX<width-width/4 && mouseY>height/3 && mouseY<height- height/3){
rectMode(CORNER);
noStroke();
fill(0,180,0,180);
for( int r = 0; r <= 7; r++){
if (r == 1)
i = 50;
rect(width/2-348,height/2-35,i,height/8-4);
if (r == 2)
i = 150;
rect(width/2-348,height/2-35,i,height/8-4);
if (r == 3)
rect(width/2-348,height/2-35,i,height/8-4);
i = 250;
if (r == 4)
rect(width/2-348,height/2-35,i,height/8-4);
i = 350;
if (r == 5)
rect(width/2-348,height/2-35,i,height/8-4);
i = 450;
if (r == 6)
rect(width/2-348,height/2-35,i,height/8-4);
i = 550;
if (r == 7)
rect(width/2-348,height/2-35,i,height/8-4);
i = 650;
}
}
}
发布于 2014-04-11 11:34:00
你想做这样的事情吗?
int time, myWidth;
boolean loading;
void setup(){
size(800,600);
loading = false;
myWidth = 0;
}
void draw(){
drawLoadingBar();
}
void drawLoadingBar(){
if(myWidth < width/3){
if(loading && millis()-time > 1000){
rect(20, height/2, myWidth, 30);
myWidth = myWidth + 10;
time = millis();
}
}
}
void mousePressed(){
if(loading == false){
time = millis();
loading = true;
}
}
此代码的工作方式是在第一次鼠标单击后每秒将条形宽度增加10。
发布于 2014-04-11 15:49:57
使用循环是在函数loop()
定义的处理中,可以由noLoop()
停止,我也使用frameCount (包含自程序启动以来显示的帧数- draw()
函数的每次运行)来计算加载进度条的百分比,并根据需要在3/4停止加载。
boolean loading = false;
int fillX = 0;
void setup()
{
size(300,300);
background(0);
noLoop();
}
void draw()
{
stroke(255);
fill(0);
rect(48, 137, 204, 25);
noStroke();
fill(255);
rect(51, 140, fillX, 20);
if(loading == true)
{
fillX = ((frameCount%301) / 3 * 2);
if(frameCount%(300*0.75) == 0)
{
loading = false;
noLoop();
frameCount = 0;
}
}
}
void mousePressed() {
loading = true;
loop();
}
RectMode(角点)是默认模式,因此不需要指定它,除非您在项目中使用不同的模式。
https://stackoverflow.com/questions/22999988
复制相似问题