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

时间:2015-10-20 15:40:45

标签: java pdf processing

我有一个类,我想用它来输出页面到pdf
foo类的每个实例都是一个单独的页面 (N.B页面/输出尺寸与屏幕不同)

如何使用自定义方法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

1 个答案:

答案 0 :(得分:2)

首先,您可能不希望从createGraphics()(类应以大写字母开头)类调用Foo,除非您需要单独的PDF文件对于每个实例!

相反,请使用草图的PGraphicsPDF函数中的draw()类的实例,然后将该实例简单地传递到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类只需要一个以display()为参数的PGraphicsPDF函数:

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