SHARE
TWEET

Untitled

a guest Aug 12th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package telmo;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.awt.print.*;
  7.  
  8. public class Printing implements Printable, ActionListener {
  9.  
  10.     public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
  11.         Graphics2D graphic = (Graphics2D)g;
  12.         graphic.translate(pf.getImageableX(), pf.getImageableY());
  13.  
  14.     String[] columnValues = {"a", "b", "c", "d"};
  15.     int width = 0, height = 0;
  16.         for (String l : columnValues) {
  17.         graphic.drawString(l, width, 135 + height);
  18.         width += 15;
  19.         height += graphic.getFont().getSize();
  20.     }
  21.         return PAGE_EXISTS;
  22.     }
  23.  
  24.     public void actionPerformed(ActionEvent e) {
  25.          PrinterJob job = PrinterJob.getPrinterJob();
  26.          job.setPrintable(this);
  27.          if (job.printDialog())
  28.              try { job.print(); } catch (PrinterException ex) {}
  29.     }
  30.  
  31.     public static void main(String args[]) {
  32.         UIManager.put("swing.boldMetal", Boolean.FALSE);
  33.         JFrame f = new JFrame("Hello World Printer");
  34.         f.addWindowListener(new WindowAdapter() {
  35.            @Override public void windowClosing(WindowEvent e) {System.exit(0);}
  36.         });
  37.         JButton printButton = new JButton("Print Hello World");
  38.         printButton.addActionListener(new Printing());
  39.         f.add("Center", printButton);
  40.         f.pack();
  41.         f.setVisible(true);
  42.     }
  43. }
RAW Paste Data
Top