首页 > 解决方案 > 如何把我画的颜料的图像放回去?

问题描述

我想做的东西类似于绘画程序。问题是当我画一些线时(不仅仅是线。我画的所有东西都包括在这种情况下。),这些线只是在我画之前画出来的。

起初,我认为这只是代码顺序的问题。但事实并非如此。

我只想在图像上画一些线条,比如绘画程序。像这样:在此处输入图像描述

标签: processing

解决方案


您可以使用PGraphics绘制到单独的“层”中。初始化实例后,您可以在beginDraw()/中使用典型的绘图方法endDraw()(如参考示例所示)。

唯一剩下的就是保存最终图像,这很简单,使用save()

这是示例 > 基础 > 图像 > LoadDisplay的修改示例,它使用单独的 PGraphics 实例在拖动鼠标时进行绘制,并在s按下键时保存最终图像:

/**
 * Based on Examples > Basics > Image > Load and Display 
 * 
 * Images can be loaded and displayed to the screen at their actual size
 * or any other size. 
 */

PImage img;  // Declare variable "a" of type PImage
// reference to layer to draw into
PGraphics paintLayer;

void setup() {
  size(640, 360);
  // The image file must be in the data folder of the current sketch 
  // to load successfully
  img = loadImage("moonwalk.jpg");  // Load the image into the program

  // create a separate layer to draw into
  paintLayer = createGraphics(width,height);
}

void draw() {
  // Displays the image at its actual size at point (0,0)
  image(img, 0, 0);
  // Displays the paint layer
  image(paintLayer,0,0);
}

void mouseDragged(){
  // use drawing commands between beginDraw() / endDraw() calls
  paintLayer.beginDraw();
  paintLayer.line(mouseX,mouseY,pmouseX,pmouseY);
  paintLayer.endDraw();
}

void keyPressed(){
  if(key == 's'){
    saveFrame("annotated-image.png");
  }
}

推荐阅读