001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Oct 9, 2009
021 * Author: Andreas Prlic
022 *
023 */
024
025package org.biojava.nbio.structure.align.gui;
026
027import javax.swing.*;
028import java.awt.*;
029import java.awt.event.ActionEvent;
030import java.awt.event.ActionListener;
031import java.awt.print.PageFormat;
032import java.awt.print.Printable;
033import java.awt.print.PrinterException;
034import java.awt.print.PrinterJob;
035
036public class JPrintPanel extends JPanel implements Printable,ActionListener{
037        /**
038         *
039         */
040        private static final long serialVersionUID = -3337337068138131455L;
041
042        @Override
043        public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
044
045                if (pi >= 1) {
046                        return Printable.NO_SUCH_PAGE;
047                }
048                Graphics2D g2D = (Graphics2D) g;
049                g.translate(20, 20);
050                Font  f = new Font("Monospaced",Font.PLAIN,10);
051                g.setFont (f);
052
053                double scale = pf.getImageableWidth()/this.getSize().getWidth();
054
055            g2D.scale(scale,scale);
056
057                paint (g);
058
059
060
061
062
063                return Printable.PAGE_EXISTS;
064        }
065
066        @Override
067        public void actionPerformed(ActionEvent e) {
068
069                PrinterJob printJob = PrinterJob.getPrinterJob();
070                printJob.setPrintable(this);
071
072                try {
073                        if(printJob.printDialog()){
074                                printJob.print();
075                        }
076                } catch (Exception printException) {
077                        System.err.println("Error during printing: " +printException.getMessage());
078                        printException.printStackTrace();
079                }
080        }
081
082}