我在一个类中有两个函数
void display()
和
void write(PGraphics pdf)
我使用 display()
在屏幕上显示元素。有没有办法调用 display()
中的代码,这样我就不必在 write(pdf)
中再次写出每个函数?例如
line(0,0,100,100)
到 pdf.line(0,0,100,100)
等
最佳答案
您可以通过绘制到 PGraphics
图像(而不是直接绘制到屏幕)来实现此目的,然后将该图像绘制到屏幕上。这样您就可以将其替换为 PDF PGraphics
,而无需更改任何代码。像这样的事情:
PGraphics pg;
PGraphics pdf;
boolean usePdf = false;
void setup() {
size(100, 100);
pg = createGraphics(width, height);
pdf = //whatever
}
void draw(){
if(usePdf){
display(pdf);
}
else{
display(pg);
image(pg, 0, 0);
}
}
void display(PGraphics g) {
g.beginDraw();
g.background(100);
g.stroke(255);
g.line(20, 20, mouseX, mouseY);
g.endDraw();
}
关于Java:如何避免 display() 和 write(pdf) 之间的代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35631891/