• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Print something in java.

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've looked into PrinterJob and PageFormat and interface Printable.

But I don't know what to put in the void print() method that my class implementing Printable is supposed to override.

basically I'm going to have a "page" with various font's and such, I'm guessing I need to put this in a Graphics Object.

and then use the print(graphics,pageformat,int);

but does java know to just print automatically, or do I need to include some code in the print() method?

here is the code I have:



Thanks,

Justin Fox
 
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the entire issue: YOU have to implement the print method. All Java does is give you a Graphics object that is directly related to the printer. If you draw on that Graphics object, it will be sent to the printer once the method returns.

You should also return either Printable.NO_SUCH_PAGE to indicate this page should be not be printed, or Printable.PAGE_EXISTS to indicate this page should be printed. What the JVM probably does is call print with an incrementing page number until it returns Printable.NO_SUCH_PAGE

For instance, to draw a single image twice all you need to do is this:
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooohhhhh ok, that makes a whole lot more sense now.

I thought you had to override that method AND call it from somewhere else.
but using the print method is like using the paintComponent method (in the sense that you dont actually have to call it, it does it's own thing).

Thanks a lot man,

Justin Fox

Oh and one more thing, when I create a font, do I place it on the Graphics object just like g.drawString()? or do I just setBounds() for that font object?

Thanks...
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do I go about envoking that print() method though? this is my print method...



but how do I make it actually print....

Do I need to make a PrinterJob instance?

Justin Fox
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, this is my code so far, it runs fine, and says it sent the job to the printer, but it never prints the graphics...

I dont know what to do...

 
Rob Spoor
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're very close it seems, but the error is in this code:

The parameter would be better be renamed to page, because it is the current page. Now you say that only the second page should be printed - but there is none.

So if you turn it into

it should work.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Again Rob,

I've read up on this "printing in Java", and more than most people agree that printing has definately been made harder to do than is neccessary.

I do appreciate you posting to my question. I've started a "business" with my friend, and we're going to try and offer small application development in Java(and other languages if need be), though website design will be the major focus.

I wanted to have a feel for everything in Java, at least in a business since.
For example, creating reports for printing, forms, etc...

I will definately apply the changes you suggested above and let you know how the App. turns out.

Thanks again,

Justin Fox
 
Marshal
Posts: 64618
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to see you have got as far as setting up a business. I can still remember when you first appeared on the Ranch.

I am considering something like that; I have got to organise records for our local Credit Union. You are going to have to work hard to persuade people that paper is not some sort of magic gold standard for record keeping.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well about the "record keeping", what this project pertains to is output of legal documents, but having labels throughout the paper that can be "dynamically" changed with certain perferences(chose from combobox's).

I was looking into different editor's such and openoffice, and their "tags", so to speak, for bold,italic,size, etc...

but that was a failure. So I figured I could set up a look-a-like graphics object of that legal document, and have string labels throughout the output.
Then when the person chose different options fromt the combobox, call repaint and the changes appear.

Either way, if someone was to ask me to implement a printing of something in java, I'll have it down pretty good .

I would have to able to preview and change printer settings, but that only involves:

PrinterJob.printDialog();
and
PrinterJob.?Dialog();

I put the ? because I can't remember the name of the function but I know there is print and another one for dialogs.

But on the job topic, I definatley feel I can do website design and things of that sort well enough to be confident in that area of work.

Right now I have a job as a programmer doing mostly database driven management software. And most of the apps are done in VB.net, but some of the older ones we still maintenance are done in Delphi4. Although this sounds impressive, I'm basically the (do stuff we don't want to do) guy. But in the end it will pay off with experience in:

1. different area of work.
2. different languages.

Then I also work as a webmaster at a local golf retail store. This is more of a joke job, I mean most people can google and read books to learn "internet programming". I just don't feel this will keep me satisfied as a "wanna-be-programmer".

I hope the business thing does work out, but around here I don't see the website/software side of the business jumping off any time soon.

Right now I mostly worried about getting experience for my resume, so If things go south, and after I graduate I don't have a job for some reason, I can fall back on my degree and "experience".

When I get to work I'll add that fix to my print app and see if the printer accepts this time.

Thanks for all the help and support,

Justin Fox
[ January 18, 2008: Message edited by: Justin Fox ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, my app actually "prints", but not. It sends a page through, but with nothing on it lol.

I think this line of code might not be refering to the right thing.



is 'this' the right variable to use? If not, how do I refer to the Graphics object 'g' in the overrid print() function?

and also, I have the same graphics drawn in paintComponent as I do in that print function, so should I remove the graphics stuff from paintComponent?

Thanks,

Justin Fox
 
Rob Spoor
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Justin Fox:
Ok, my app actually "prints", but not. It sends a page through, but with nothing on it lol.

I think this line of code might not be refering to the right thing.



is 'this' the right variable to use? If not, how do I refer to the Graphics object 'g' in the overrid print() function?


I've copied your earlier code, modified only the "if(pages == 1)" part into "if(pages == 0)", then ran it - and it printed a single line. Are you sure it's not a problem with the printer? I used CutePDF to print to PDF.

and also, I have the same graphics drawn in paintComponent as I do in that print function, so should I remove the graphics stuff from paintComponent?


Well paintComponent is responsible for painting the panel, so if you remove it there it will not print anything on the panel itself.

What you COULD do is remove all drawing code from the print(...) method and replace it with a single call to "paintComponent(g);". This will work because the Graphics object is still the printer Graphics object, and not the screen Graphics object which is used by screen painting.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I changed the x,y coordinate to 15,25 instead of 0,0 and it printed fine...

Is there a way to make margins for that page? and word-wrap for the drawString() ?

Also, instead of having to do drawString multiple times to make separate lines, is there some function for that?

I mean, having to modify the 'y' coordinate of the text along the y-axis gets kinda cruddy after awhile, especially if I need a pretty lengthy file.

Justin Fox
 
Rob Spoor
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the real margins of the page from the PageFormat object: getImageableX and -Y. Similarly, getImageableWidth and -Height return the size of the page you can print on.

As for word-wrap, there is no real solution for that out-of-the-box.
Here's a way to possibly solve it:
- get the FontMetrics from the Graphics object (getFontMetrics())
- use its stringWidth and getHeight methods to determine where to stop / start. You might also find a use for the getStringBounds method that takes a start and end offset of the string.

Now the second part may be quite a pain; do you try the stringWidth for the full string first, then try cutting of pieces at the end? Or do you use getStringBounds, decreasing the end offset until the Rectangle fits?

These are parts I can't really help you with.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm definately going to look into the margin stuff, and the width/height of the string.

Ok, this is what I'm wanting to do.

- I have a legal document I want to get line by line, and write it
to the graphics object(font/size/weight).

if I just use the text from the document, shouldn't the text fit good on the paper? Since the document is already set up and margined out?

I'm thinking this will work, but there may be problems with different printers.

but if I were to get the margins, left,right,top and bottm, I could just paste the lines of text from the document to my graphics and apply those margins before, and everything SHOULD look ok right?


I mean, I'm not saying I can just take the text, draw it to the graphics and everything work. I will need to apply my margins first, but since the text from the document will print on the page properly, if I apply the same margins from the document to the graphics and THEN paste the text from the document to the graphics, would this work?

Thanks for all the help,

Justin Fox
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!