Click to See Complete Forum and Search --> : Java and printing
TheLinuxDuck
04-04-2002, 05:51 PM
I was wondering if anyone has any experience printing graphics with java?
I've got an app that I wrote whose sole purpose is to print graphics. Well, I assumed , in my own stupidity, that the graphics part would work nicely.
Wasn't I surprised when I finally got to that part!!!
The images I have printed look horrible. Absolutely horrible. And, I don't know any way to adjust any settings or anything to improve upon this.
If anyone has some words of wisdom, in regards to 1.4, I would truely appreciate it! I've got an app that is useless until this part of it is added in! (^=
/me hides his duck head in shame
Stuka
04-05-2002, 01:05 PM
How are you trying to print this? I'm looking at the API docs for javax.print, and it seems kinda tough - what are you trying to print?
TheLinuxDuck
04-05-2002, 01:44 PM
For right now, I just simply want to be able to print out a graphic, and not have it look like crud. Every time I've tried printing a graphic (from a jpeg), it looks horrible. The images need to be of a certain quality (better than screen rez) to work for the print.. I was under the impression that java did not use screen rez, but actual image rez to print.
I'd be highly disappointed to find out otherwise.
Stuka, thanks for the reply. I don't know that I will find an answer. This project is quite old now, and has since lost it's luster. (^=
Stuka
04-05-2002, 02:36 PM
Well, the format of the example is kinda rough, but I found this example (http://java.sun.com/products/jfc/tsc/articles/javaOne2001/595/index.html) on Sun's site. I'll see if I can't dl the 1.4 stuff and play with it - if I come up with a decent answer, you'll be the first to know!
<edit>
OK, I dl'ed 1.4.0, and read over the docs, and this is what I came up with. It printed a pretty decent copy of the (small) .jpg, considering it was full color printing on a non-color laser printer. Here's the code://Testing javax.print API for TLD
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;
public class printTest{
public static void main(String[] args){
try{
SimpleDoc doc = new SimpleDoc(new FileInputStream("test.jpg"), DocFlavor.INPUT_STREAM.JPEG, null);
PrintService pr = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = pr.createPrintJob();
job.print(doc, null);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Give it a whirl and let me know how it works for you!
[ 05 April 2002: Message edited by: Stuka ]
TheLinuxDuck
04-08-2002, 11:45 AM
Well.. that example worked very well, as a matter of fact.. (^=
Now, my next step is to set it up to allow me to choose which printer I want to use. I've got other stuff to add in, and test, but we'll go one step at a time here.. (^=
Thanks for your help, dood!
Stuka
04-09-2002, 12:17 PM
In that case, you'll want to use this function:public static final PrintService[] lookupPrintServices(DocFlavor flavor, AttributeSet attributes) I'm not sure how it will work exactly, but I'm betting you can work it out...
<edit>
You're more than welcome for the help - as often as you've helped out around here, someone oughta return the favor!
Also, it looks like the names of the printers (on my Win2K system here @ work) are of this format - "Win32 Printer:<Printer Model & Name>".
</edit>
[ 09 April 2002: Message edited by: Stuka ]
TheLinuxDuck
04-09-2002, 12:26 PM
Stuka:
I did actually replace the DefaultPrintService line with that call, and my app crashes... (^= Not sure why..
I haven't done a lot of playing with it, though.
Stuka
04-09-2002, 03:49 PM
What kind of error messages do you get on the crash? I've discovered that Java error messages are quite handy if you read them thoroughly. I used that function (but notice it returns an array!) to enumerate the printers on my system that would handle a JPG, and it happily answered. I didn't actually select one, but I could probably build a test dialog box or frame that did that for you if you'd like...
TheLinuxDuck
04-09-2002, 05:39 PM
Here's my code:
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.io.*;
public class PrintTest {
public static void main(String[] args) {
try {
SimpleDoc doc = new SimpleDoc(new FileInputStream("test.jpg"),
DocFlavor.INPUT_STREAM.JPEG, null);
//PrintService pr = PrintServiceLookup.lookupDefaultPrintService();
PrintService[] pr = PrintServiceLookup.lookupPrintServices(DocFlavor.I NPUT
_STREAM.JPEG, null);
if(pr.length == 0) {
System.out.println("No matching services");
System.exit(1);
}
System.out.println("pr has " + pr.length + " items");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
And, as you can tell, it doesn't do anything except create the image doc thing, then look for available printers. What happens is, it pops up a close/ignore "An error has occured in your program. To keep working..blah..blah.." dialog, then once I close it, the java app does an illegal exception, and that's the end of the story.
Choosing ignore does nothing but lock up java.
So.. maybe there's some preliminary setup I need to do with printing devices before I can find a service.. I dunno.. like I said, I haven't done a lot of testing..
Stuka
04-10-2002, 11:37 AM
I'm not sure what the problem is. That code works perfectly on my system, as does a variation I had that actually listed the items returned by lookupPrintServices(). Very odd.... which version of the SDK are you using? I've got build 1.4.0b92, running on Win32 - could it be a platform- or build-specific bug?
TheLinuxDuck
04-10-2002, 11:41 AM
Hmm.. I'm also running 1.4.0b92 on a win32 system.
And I've run 1.4.0beta on here before (many months ago) and it worked ok.
maybe I just need to reinstall java...
hmm.. dunno,.
Stuka
04-10-2002, 12:48 PM
Hmmm....that is odd. It really does sound like a JVM error - I'm guessing from your description that those are Windows error boxes (since you don't have a GUI on yer Java...), so maybe a reinstall is in order...very annoying.