Print current sketch
in
Programming Questions
•
2 years ago
Hi all,
I just made a sketch which I want to print. I'm puzzeling with Java's Print but I can't get it right..
Class:
- public class HelloWorldPrinter implements Printable {
- public int print(Graphics g, PageFormat pf, int page) throws
- PrinterException {
- if (page > 0) { /* We have only one page, and 'page' is zero-based */
- return NO_SUCH_PAGE;
- }
- /* User (0,0) is typically outside the imageable area, so we must
- * translate by the X and Y values in the PageFormat to avoid clipping
- */
- Graphics2D g2d = (Graphics2D)g;
- g2d.translate(pf.getImageableX(), pf.getImageableY());
- /* Now we perform our rendering */
- // g.drawString("Hello world!", 100, 100);
- // g.drawString(day() + " " + month(), 100, 120);
- frame.printAll(g);
- /* tell the caller that this page is part of the printed document */
- return PAGE_EXISTS;
- }
- }
- import java.io.File;
- import java.awt.print.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- PImage[] IMGs;
- int minWidth = -1;
- ArrayList parts;
- void setup(){
- PrinterJob job = PrinterJob.getPrinterJob();
- job.setPrintable(new HelloWorldPrinter());
- PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
- parts = new ArrayList();
- ***** CODE *****
- println("Partssize: " + parts.size());
- for(int i = 0; i < parts.size(); i++){
- IMG part = (IMG) parts.get(i);
- image(part.img, 0, i, width, 1);
- }
- saveFrame();
- try {
- println("Trying to print..");
- job.print();
- println("Printed");
- }
- catch (PrinterException e) {
- /* The job did not successfully complete */
- println("Error:");
- println(e);
- }
- }
Can someone explain to me what I'm doing wrong? I allready found that Java Print uses Graphic2D but I can't manage to convert the sketch to Graphic2D..
1