processing - 区域点击处理后在屏幕上绘图
问题描述
我正在努力做到这一点,以便当用户单击程序中的铅笔图标时,他们被允许绘制,直到再次单击铅笔。我还没有在切换中添加,但我目前的问题是我在我指定的鼠标区域中获得了所需的行为,但在屏幕上没有其他地方,还请原谅我的代码在玩它时会发生很多变化,但是通过运行它在当前状态下,您应该看到我在说什么错误。
谢谢
PImage[] img = new PImage[3];
boolean drawPencil = false;
boolean draw = false;
void setup() {
size(960, 720);
for (int i = 0; i < img.length; i++)
img[i] = loadImage("image"+i+".png");
//an array of images that sorts and load them into our program
image(img[0],0,0);
img[0].resize(width, height);
// set the desired image to screen size
}
void mousePressed() {
if (mouseX > 772 && mouseX < 864 && mouseY > 1 && mouseY < 74) {
drawPencil = true;
draw = true;
// if the user clicks the pencil icon they will begin to draw
}
}
void mouseReleased()
{
draw = false;
}
void draw() {
if (draw) {
stroke(255);
line(mouseX, mouseY, pmouseX, pmouseY);
}
if (key == 'w'||key == 'W') {
image(img[0],0,0);
img[0].resize(width, height);
println(drawPencil);
}
}
解决方案
比上一个帖子好多了。我想我现在得到了你想要的。我为您编写了一个简短的示例,以便您掌握它。下面是它的作用:
PImage img;
boolean canDraw;
void setup() {
size(960, 720);
background(255);
img = loadImage("pencil.png");
}
void draw() {
if (canDraw) {
fill(color(0, 128, 0));
} else {
fill(color(128, 0, 0));
}
rect(800, 1, 100, 100);
image(img, 800, 1, 100, 100);
if (canDraw && mousePressed) {
stroke(0);
line(mouseX, mouseY, pmouseX, pmouseY);
}
}
void mouseClicked() {
if (mouseX > 800 && mouseX < 800+img.width && mouseY > 1 && mouseY < 1+img.height) {
canDraw = !canDraw;
}
}
如果你想问这个问题,我会在附近闲逛。玩得开心!
推荐阅读
- javascript - 以数组为参数并生成随机数的函数
- xquery - 在 MarkLogic 中为日期设置动态分桶约束
- javascript - 如何从 JavaScript 的 Math.random 生成包含有界的帕累托随机整数
- javascript - JavaScript 运行时错误:[Messenger] 未提供必需的属性“目标”
- typescript - 约束键,使其只能引用 TItem 的数字或日期键
- ruby-on-rails - Rails 字体 CORS 策略
- azure - 用户 $Skip 和 $Count
- rspec - Capybara 在 Accept_Confirm 上失败
- powershell - Get-ADComputer 返回与查询不同的属性?
- oop - 子类的关联类是否继承超类?