2015-10-20 73 views
0

我有,我想用来输出页面的PDF
foo类的每个实例将是一个单独的页面类
(NB页/输出大小不一样的屏幕)

如何使用自定义方法display()打印到每个页面?
还是我弄错了?在处理与方法采用的createGraphics输出多页PDF

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 

回答

2

首先,你可能不想叫你从里面FoocreateGraphics()(类应该有一个大写字母开头),除非你想为每个实例单独的PDF文件!

而是使用草图的draw()函数中的PGraphicsPDF类的实例,然后将该实例简单地传递到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 
    } 
} 
+0

嘿凯文 感谢您的回答!关于'foo'需要成为'Foo',你是完全正确的......将'PGraphicsPDF'作为参数传递是一个很好的解决方案,然后我将它传递给嵌套对象。我认为在决定是否为每个“Foo”实例创建一个单独的pdf之前,我需要考虑一下我想从另一端得到多少东西,这两者对我都有好处。在真实的代码中,“Foo”无论如何都是一个页面,所以它可能是有道理的。 – jackoverflow