1

for my current project, I'm looking for a really simple workaround. I do a random graphic in Processing, and when the code has finished the graphic it should print on my printer. But preferable without the dialog etc. Just print it on paper.

I was looking around the Internet and several Java forums, but I only found "overkill" tutorials. (like this: http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print.html)

Is there an easy way to do this?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
nbuechi
  • 229
  • 4
  • 20

4 Answers4

0

If there is no easy way to directly call the printer, you may use the java Robot class, here is an example on how to use it: How to simulate keyboard presses in java?

Community
  • 1
  • 1
Pwdr
  • 3,712
  • 4
  • 28
  • 38
0

I'm answering this 8 years late, but hopefully it helps anyone who finds this thread.

First save the image / frame that you want to print with a definite path:

save(“image_to_print.png”);

Then add this method to your sketch:

void printImage(String path) {  
  Process p = exec("lp", path); 
  try {
    int result = p.waitFor();
    println("the process returned " + result);
  } 
  catch (InterruptedException e) {
    println("error : " + e);
  }
}

Now just call the method with the relevant filename and path:

printImage("/Users/me/Desktop/printDemo/image_to_print.png");

It's the equivalent of running the following in your command line interface:

lp image_to_print.txt

Here's a medium post explaining this.

Mithru
  • 101
  • 2
  • 4
0

I've heard good things about GDSPrinting. While I've not used it and according to that page it's version 0.2, it may be a solution for you.

As has been said too many times before: Java and Printers don't get along very well. I wish you luck.

Zéychin
  • 4,135
  • 2
  • 28
  • 27
0

This is definitely a hack, but if you can't find what you're looking for, script taking a screenshot (keyboard command) and open a photo editor and print. http://sikuli.org/ can automate pressing buttons (like print) and you can execute external commands from Processing using open()

http://processing.org/reference/open_.html

http://sikuli.org/docx/faq/010-command-line.html

Ptterb
  • 336
  • 2
  • 7