java 打印机程序

 import java.awt.BorderLayout;

import java.awt.Canvas;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D.Double;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
 
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
 
import com.google.common.annotations.GwtCompatible;
import com.sun.xml.internal.fastinfoset.tools.PrintTable;
 
 
public class PrintTest {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame frame = new PrintTestFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
 
}
 
class PrintTestFrame extends JFrame {
    public PrintTestFrame() {
        // JFrame 继承于Frame,而setTitle是Frame的一个方法
        setTitle("PrintTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        
        canvas = new PrintPanel();
        add(canvas, BorderLayout.CENTER);
        
        attributes = new HashPrintRequestAttributeSet();
        
        JPanel buttonPanel = new JPanel();
        JButton printButton = new JButton("Print");
        buttonPanel.add(printButton);
        printButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent event) {
                try{
                    PrinterJob job = PrinterJob.getPrinterJob();
                    job.setPrintable(canvas);
                    if(job.printDialog(attributes))
                        job.print(attributes);
                } catch (PrinterException e) {
                    JOptionPane.showMessageDialog(PrintTestFrame.this, e);
                }
            }
        });
        
        JButton pageSetUpButton = new JButton("Page setup");
        buttonPanel.add(pageSetUpButton);
        pageSetUpButton.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent event) {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.pageDialog(attributes);
            }
        });
        
        add(buttonPanel, BorderLayout.NORTH);
    }
    
    private PrintPanel canvas;
    private PrintRequestAttributeSet attributes;
    
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 300;
}
 
class PrintPanel extends JPanel implements Printable{
 
    @Override
    public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException {
        // TODO Auto-generated method stub
        if (page >= 1) return Printable.NO_SUCH_PAGE;
        Graphics2D g2 = (Graphics2D) g;
        g2.translate(pf.getImageableX(), pf.getImageableY());
        g2.draw(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight()));
        drawPage(g2);
        return Printable.PAGE_EXISTS;
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        drawPage(g2);
    }
    
    public void drawPage(Graphics2D g2) {
        FontRenderContext context = g2.getFontRenderContext();
        Font f = new Font("Seriv", Font.PLAIN, 72);
        GeneralPath clipShape = new GeneralPath();
        
        TextLayout layout = new TextLayout("Hello", f, context);
        AffineTransform transform = AffineTransform.getTranslateInstance(0, 72);
        Shape outline = layout.getOutline(transform);
        clipShape.append(outline, false);
        
        layout = new TextLayout("world", f, context);
        transform = AffineTransform.getTranslateInstance(0, 144);
        outline = layout.getOutline(transform);
        clipShape.append(outline, false);
        
        g2.draw(clipShape);
        g2.clip(clipShape);
        
        final int NLINES = 50;
        Point2D p = new Point2D.Double(0, 0);
        for(int i = 0; i < NLINES; i++) {
            double x = (2 * getWidth() * i)/NLINES;
            double y = (2 * getHeight() * (NLINES-1-i))/NLINES;
            Point2D q = new Point2D.Double(x, y);
            g2.draw(new Line2D.Double(p, q));
        }
    }
    
}

你可能感兴趣的:(java,java打印机程序)