java - 无法获得 mouseClicked 以随机播放不同的图像
问题描述
我正在做一个项目,该项目必须编写不同的用于绘制线条的循环,以便在单击时循环将切换到下一个。从代码中可以看出,每个方法都应该在计数到达时出现,然后在计数变高时消失。所需的输出是在播放时它应该显示第一个方法 hbars 并且单击时它将转到 vbars 并再次单击到 geoLines 等等,直到用户定义到达那里后它就完成了。现在,如果我玩它,它会在一秒钟内完成所有方法,而无需我点击。抱歉,如果没有完整的代码,我无法真正解释大量代码。
int count=0;
void setup()
{
size(800, 600);
}
void draw()
{
mouseClicked();
}
void hbars()
{
background(0);
fill(255);
for (int y=0; y <height; y+=height/10)
{
rect(0, y, width, height/20);
}
}
void vbars()
{
background(0);
fill(255);
for (int x = 0; x<width; x+=width/8)
{
rect(x, 0, width/16, height);
}
}
void diagonalLines()
{
background(0);
fill(255);
for (int i=0; i<=width*2; i+=width/100)
{
line(0, i, i, 0);
}
}
void geoLines()
{
background(0);
int hx = 0;
int hy = 0;
int vx = width;
int vy = height;
for (int x =0; x < 10; x++)
{
line(0, hy, hx, height);
line(vx, height, 0, vy);
hy+=height/20;
hx+=width/20;
vx-=width/20;
vy-=height/20;
}
}
void circles()
{
background(0);
fill(255);
for (int i=0; i<100; i++)
{
noFill();
circle(width/2, height/2, i*10);
}
}
void fadeRightToLeft(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeLeftToRight(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeDown()
{
background(0);
for (int i=0; i <40; i++) {
if (i%2==1) {
fadeRightToLeft(i*40);
} else
{
fadeLeftToRight(i*40);
}
}
}
void checkerboard()
{
background(0);
int count = 0;
int row = 0;
for (int r = 0; r < 8; r++)
{
for (int c = 0; c < 8; c++)
{
if (count % 2 == 0)
{
if (row % 2 == 0)
{
fill(255);
} else fill(0);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
} else
{
if (row % 2 == 0)
{
fill(0);
} else fill(255);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
}
}
row++;
}
}
void userDefined()
{
background(0);
for (int i = 0; i<100; i++)
{
stroke(3*i, 160*i, 15*i);
noFill();
circle(0+(i*7.5), 0+(i*7.5), i*12);
circle(0+(i*7.5), 600+(i*7.5), i*40);
}
}
void mouseClicked()
{
count++;
if (count==1)
hbars();
if (count==2)
vbars();
if (count==3)
diagonalLines();
if (count==4)
geoLines();
if (count==5)
circles();
if (count==6)
fadeRightToLeft(0);
if (count==7)
fadeLeftToRight(0);
if (count==8)
fadeDown();
if (count==9)
checkerboard();
if (count==10)
userDefined();
}
解决方案
您几乎拥有它,您通常只需要避免将调用mouseClicked()
置于draw()
循环中。
为什么它不能正常工作?该mouseClicked()
方法像一个事件一样工作,这意味着它会在特定事件发生时动态调用(在本例中为鼠标单击)。您不需要调用它 - 作为一般规则,您永远不应该以编程方式调用事件处理程序。
draw()
循环每秒运行大约 60 次。由于它正在调用该方法以切换到下一个背景,因此在几分之一秒后它就用完了背景。
我修复了您的代码并修改了一些细节(我对此进行了评论,以便您知道原因)。干得好:
int count=0;
void setup()
{
size(800, 600);
// I think that we can call the first background before a user input, but it's your call
NextBackground();
}
void draw()
{
// mouseClicked(); // mouseClicked is called as an event, do not call it programatically (also yyou don't need to)
// Processing wants it's draw() loop so we'll let it here even if it's empty.
}
void hbars()
{
background(0);
fill(255);
for (int y=0; y <height; y+=height/10)
{
rect(0, y, width, height/20);
}
}
void vbars()
{
background(0);
fill(255);
for (int x = 0; x<width; x+=width/8)
{
rect(x, 0, width/16, height);
}
}
void diagonalLines()
{
background(0);
fill(255);
for (int i=0; i<=width*2; i+=width/100)
{
line(0, i, i, 0);
}
}
void geoLines()
{
background(0);
int hx = 0;
int hy = 0;
int vx = width;
int vy = height;
for (int x =0; x < 10; x++)
{
line(0, hy, hx, height);
line(vx, height, 0, vy);
hy+=height/20;
hx+=width/20;
vx-=width/20;
vy-=height/20;
}
}
void circles()
{
background(0);
fill(255);
for (int i=0; i<100; i++)
{
noFill();
circle(width/2, height/2, i*10);
}
}
void fadeRightToLeft(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeLeftToRight(int height)
{
background(0);
for (int i = 0; i<100; i++)
{
noStroke();
fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
rect(0+(i*7.5), 0+height, width/10, 50);
}
}
void fadeDown()
{
background(0);
for (int i=0; i <40; i++) {
if (i%2==1) {
fadeRightToLeft(i*40);
} else
{
fadeLeftToRight(i*40);
}
}
}
void checkerboard()
{
background(0);
int count = 0;
int row = 0;
for (int r = 0; r < 8; r++)
{
for (int c = 0; c < 8; c++)
{
if (count % 2 == 0)
{
if (row % 2 == 0)
{
fill(255);
} else fill(0);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
} else
{
if (row % 2 == 0)
{
fill(0);
} else fill(255);
square(row*width/10.7, c*width/10.7, width/10.7);
count++;
}
}
row++;
}
}
void userDefined()
{
background(0);
for (int i = 0; i<100; i++)
{
stroke(3*i, 160*i, 15*i);
noFill();
circle(0+(i*7.5), 0+(i*7.5), i*12);
circle(0+(i*7.5), 600+(i*7.5), i*40);
}
}
void mouseClicked()
{
// I moved this code into it's own method as the mouseClicked method may serve other purposes
NextBackground();
}
void NextBackground() {
count++;
if (count > 10) {
count = 1;
// this way you loop in your backgrounds
}
// a bunch of "if" will get the job done, but you should use a switch for that part:
switch (count) {
case 1:
hbars();
break;
case 2:
vbars();
break;
case 3:
diagonalLines();
break;
case 4:
geoLines();
break;
case 5:
circles();
break;
case 6:
fadeRightToLeft(0);
break;
case 7:
fadeLeftToRight(0);
break;
case 8:
fadeDown();
break;
case 9:
checkerboard();
break;
case 10:
userDefined();
break;
default:
// this one will be used for any other value, which should never happen, but I'm putting it so you know about it
hbars();
}
}
玩得开心!
推荐阅读
- asynchronous - Ionic -Typeerror: .then() 不是函数
- html - 如何编辑下拉选择选项或将选择选项的类型更改为文本类型
- mysql - 如何连接两个 mysql 表并从连接表中检索最新结果?
- java - 使用我的程序逻辑时获得空输出
- php - 我正在尝试使用 .htaccess 更改我的网址,但遇到了一些问题
- shell - Shell脚本 - 根据文件名替换目录中所有文件中的字符串
- java - 在 JOOQ 中添加要删除的 order by 子句
- c++ - 使用 dlmopen 打开的库的调试策略
- wordpress - 无法在投资组合部分调整图像大小
- java - Docker 和设置 JVM 参数