stirman
September 21, 2018, 6:39pm
1
Is it possible to render a drawing to a multiple page PDF and draw the image to the screen?
I can do either or, obviously, but the combination errors out on this line in the draw loop…
PGraphicsPDF pdf = (PGraphicsPDF) g;
With this error…
ClassCastException: processing.awt.PGraphicsJava2D cannot be cast to processing.pdf.PGraphicsPDF
Any suggestions?! Many thanks in advance
tony
September 22, 2018, 4:18pm
2
Can I ask you to provide enough code to reproduce the error?
1 Like
stirman
September 22, 2018, 5:05pm
3
import processing.pdf.*;
void setup() {
size(200, 200);
beginRecord(PDF, "test.pdf");
}
void draw() {
PGraphicsPDF pdf = (PGraphicsPDF) g; // Get the renderer
for(int i=0; i<5; i++) {
line(i,i,5-i,5-i);
pdf.nextPage();
}
endRecord();
exit();
}
Error: “ClassCastException: processing.awt.PGraphicsJava2D cannot be cast to processing.pdf.PGraphicsPDF” on the first line in draw().
Thanks for taking a look!
1 Like
stirman
September 22, 2018, 11:50pm
5
quark
September 23, 2018, 9:31am
6
You are not using the PDF library properly. Look at the examples that come with the library, play with them so you understand how PDF Export works.
You can find the examples
select File > Examples from the menus
in the Java Examples window expand the Libraries tab and then the PDF Export tab
2 Likes
quark
September 23, 2018, 10:34am
7
tony:
What do you declared g
?
g
is the graphics renderer used with this sketch and is declared in the PApplet
class. It is of type PGraphics
so can reference any object of type PGraphics
or a child class of PGraphics
.
The sketch code above uses the PGraphicsJava2D
renderer so g
references a PGraphicsJava2D
object. If the size() method specified P2D or P3D it would reference a PGraphicsOpenGL
object and in JavaFX mode it would reference a PGraphicsFX2D
object.
4 Likes
stirman
September 24, 2018, 9:43pm
8
I didn’t know the Examples existed! Very helpful, thank you!
1 Like
Freek
November 23, 2022, 5:04pm
9
The PGraphicsPDF
object gets returned by the beginRecord
method:
import processing.pdf.*;
PGraphicsPDF graphicsPDF;
void setup() {
size(200, 200);
graphicsPDF = beginRecord(PDF, "test.pdf");
}
void draw() {
for(int i=0; i<5; i++) {
line(i,i,5-i,5-i);
graphicsPDF.nextPage();
}
endRecord();
exit();
}