我有一个类,我想用它来输出页面到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
答案 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
}
}