微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

区域单击处理后在屏幕上绘制

如何解决区域单击处理后在屏幕上绘制

我正在尝试这样做,以便用户在程序中单击铅笔图标时,他们可以绘画,直到再次单击铅笔为止。我尚未在切换器中添加IVE,但是我当前的问题是我在指定的鼠标区域中获得了所需的行为,但在屏幕上没有其他地方,也请原谅我的代码在运行时发生了很多变化,但是通过运行它在当前状态下,您应该看到我在说什么错误

谢谢

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);
  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],height);
  
  println(drawPencil);
  }


}

解决方法

发布方式比上一个更好。我想我现在得到您想要的。我为您编写了一个简短的示例,因此您可以掌握它。这是它的作用:

drawing around

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));
  }
  rect(800,1,100,100);
  image(img,800,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;    
  }
}

如果您想问这个问题,我会闲逛的。玩得开心!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。