Component ng = NengoGraphics.getInstance();
if (fileChooser.showSaveDialog(ng)==JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Universe universe = ((NengoGraphics) ng).getUniverse();
double w = universe.getSize().getWidth();
double h = universe.getSize().getHeight();
// Top of page method: prints to the top of the page
float pw = 550;
float ph = 800;
// create PDF document and writer
Document doc = new Document();
try{
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(file));
doc.open();
PdfContentByte cb = writer.getDirectContent();
// create a template
PdfTemplate tp = cb.createTemplate(pw,ph);
Graphics2D g2 = tp.createGraphicsShapes(pw,ph);
// scale the template to fit the page
AffineTransform at = new AffineTransform();
float s = (float) Math.min(pw/w,ph/h);
at.scale(s,s);
g2.setTransform(at);
// print the image to the template
// turning off setUseGreekThreshold allows small text to print
Text.setUseGreekThreshold(false);
universe.paint(g2);
Text.setUseGreekThreshold(true);
g2.dispose();
// add the template
cb.addTemplate(tp,20,0);