首页 > 解决方案 > 区域点击处理后在屏幕上绘图

问题描述

我正在努力做到这一点,以便当用户单击程序中的铅笔图标时,他们被允许绘制,直到再次单击铅笔。我还没有在切换中添加,但我目前的问题是我在我指定的鼠标区域中获得了所需的行为,但在屏幕上没有其他地方,还请原谅我的代码在玩它时会发生很多变化,但是通过运行它在当前状态下,您应该看到我在说什么错误。

谢谢

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);
  }


}

标签: processing

解决方案


比上一个帖子好多了。我想我现在得到了你想要的。我为您编写了一个简短的示例,以便您掌握它。下面是它的作用:

到处画

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;    
  }
}

如果你想问这个问题,我会在附近闲逛。玩得开心!


推荐阅读