Processing with Methods中使用CreateGraphics输出多页PDF

Using CreateGraphics in Processing with Methods to output multipage PDF

我有一个 class,我想用它来将页面输出为 pdf
foo class 的每个实例将是一个单独的页面
(N.Bpage/output尺寸与屏幕不一样)

如何使用自定义方法 display() 打印到每一页?
还是我都弄错了?

class foo{

int fooWidth, fooHeight;
ArrayList<goo> gooList;


foo(){
//constructors etc
}

void display(){
//display stuff
//calls nested goo objects in Arraylist and displays them too
}

void output(){
PGraphics pdf = createGraphics(fooWidth, fooHeight, PDF, "foo.pdf");
pdf.beginDraw();

///display() code here

pdf.dispose();
pdf.endDraw();

}//foo class

首先,您可能不想从 Foo 内部调用 createGraphics()(classes 应该以大写字母开头)class,除非您希望每个实例都有一个单独的 PDF 文件!

相反,使用草图 draw() 函数中 PGraphicsPDF class 的一个实例,然后将该实例简单地传递给 Foo 的每个实例。它可能看起来像这样:

ArrayList<Foo> fooList = new ArrayList<Foo>();

void setup() {
  size(400, 400, PDF, "filename.pdf");
  //populate fooList
}

void draw() {

  PGraphicsPDF pdf = (PGraphicsPDF) g;  // Get the renderer

  for(Foo foo : fooList){
    foo.display(pdf); //draw the Foo to the PDF
    pdf.nextPage();  // Tell it to go to the next page
  }
}

(代码基于 this 页面上的参考)

然后你的 Foo class 只需要一个 display() 函数以 PGraphicsPDF 作为参数:

class Foo{
   public void display(PGraphicsPDF pdf){
      pdf.ellipse(25, 25, 50, 50);
      //whatever
   }
}