Printing in awt

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 8
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Printing in awt

 
0
  #1
Sep 1st, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 18
Reputation: hivework is an unknown quantity at this point 
Solved Threads: 0
hivework hivework is offline Offline
Newbie Poster

Re: Printing in awt

 
0
  #2
Sep 1st, 2004
Here is a very basic example of how to print out a line of text in Java

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC