//JDK1.4
import javax.print.*;
import javax.print.event.*;
import javax.print.attribute.*;
import java.awt.print.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class PrintPrintable {   static class MyComponent extends JPanel
       implements Printable {     Font theFont = new Font("Serif", Font.ITALIC, 48);     public void paint(Graphics g) {
       super.paint(g);
       String msg = "你好,世界!";       g.setFont(theFont);
       FontMetrics fm = g.getFontMetrics();       // Center line
       int width = getWidth();
       int stringWidth = fm.stringWidth(msg);
       int x = (width - stringWidth)/2;       int height = getHeight();
       int stringHeight = fm.getHeight();
       int ascent  = fm.getAscent();
       int y = (height - stringHeight)/2 + ascent;       g.drawString(msg, x, y);
       g.drawRect(x, y-ascent, stringWidth, stringHeight);     }

     public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
   int x = (int)pageFormat.getImageableX();
       int y = (int)pageFormat.getImageableY();
       g.translate(x, y);
       if (pageIndex == 0) {
         paint(g);
         return Printable.PAGE_EXISTS;
       } else {
         return Printable.NO_SUCH_PAGE;
       }
     }
   }   public static void main(String args[]) throws Exception {     final JFrame frame = new JFrame("Printing Graphics");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     Container contentPane = frame.getContentPane();     final Component printIt = new MyComponent();
     contentPane.add(printIt, BorderLayout.CENTER);     JButton button = new JButton("打印");
     contentPane.add(button, BorderLayout.SOUTH);     ActionListener listener = new ActionListener() {
       public void actionPerformed(ActionEvent e) {  //文档风格
         DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
 //打印请求参数
 PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();  //所有printService
 PrintService[] printService = PrintServiceLookup.lookupPrintServices(null,null);
 //默认printService
         PrintService defaultPrintService =
           PrintServiceLookup.lookupDefaultPrintService();
 //打印设置对话框
 PrintService service = ServiceUI.printDialog(
 null, 20, 20,printService, defaultPrintService, flavor, pras);         DocPrintJob job = service.createPrintJob();
         PrintJobListener pjlistener = new PrintJobAdapter() {
           public void printDataTransferCompleted(PrintJobEvent e) {
             System.out.println("Good-bye");
             System.exit(0);
           }
         };
         job.addPrintJobListener(pjlistener);
         DocAttributeSet das = new HashDocAttributeSet();
         Doc doc = new SimpleDoc(printIt, flavor, das);
         try {
           job.print(doc, pras);
         } catch (PrintException pe) {
           pe.printStackTrace();
         }
       }
     };
     button.addActionListener(listener);     frame.setSize(350, 250);
     frame.show();
   }
}