944,159 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3877
  • Java RSS
Sep 1st, 2004
0

Printing in awt

Expand Post »
Hi everyone,
I want to know how to print the contents of the text area including all its fonts to the printer. I have no idea how to implement this in awt. Could someone show me or e-mail me any sample codings on how the print functionality
can be implemented in awt and tell me what is the name of the api that i should be looking at.

My e-mail is freesoft_2000@yahoo.com

I really hope someone can help me

Yours Sincerely

Richard West
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Sep 1st, 2004
0

Re: Printing in awt

Here is a very basic example of how to print out a line of text in Java

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import java.awt.print.*;
  4. /**
  5.  Simple printing application that
  6.  will demonstrate how to print out simple text
  7. */
  8.  
  9. class MyPage implements Printable
  10. {
  11. public void paint (Graphics g) // This method will define what will
  12. { // be printed on paper
  13. Graphics2D g2d = (Graphics2D) g;
  14. g2d.setFont(new Font("Arial", Font.BOLD, 30));
  15. g2d.drawString ("Hello World of Paper!", 200, 150);
  16.  
  17. }
  18. public int print (Graphics g, PageFormat f, int i)
  19. {
  20. if (i != 0) // To prevent overflow. i indicates number of pages to print
  21. return NO_SUCH_PAGE;
  22. paint (g); // Call paint method
  23. return PAGE_EXISTS;
  24. }
  25. }
  26. class MyPrinter
  27. {
  28. public static void main (String[] args)
  29. {
  30. PrinterJob job = PrinterJob.getPrinterJob();
  31. System.out.println ("Staring printer...");
  32. job.setPrintable(new MyPage());
  33. if (job.printDialog()) // Show the printing dialog
  34. {
  35. try {
  36. job.print(); // Attempt printing
  37. }
  38. catch (PrinterException e)
  39. {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. }
Reputation Points: 13
Solved Threads: 0
Newbie Poster
hivework is offline Offline
18 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Always on top Frame
Next Thread in Java Forum Timeline: problem with lengthy query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC