From: amwaterhouse Date: Fri, 18 Feb 2005 12:27:04 +0000 (+0000) Subject: tree is printable X-Git-Tag: Release_2_0~651 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=4ea7364b143ad06a38fb9ce4c39e565ebe1ee9fb;p=jalview.git tree is printable --- diff --git a/src/jalview/gui/TreeCanvas.java b/src/jalview/gui/TreeCanvas.java index c39e407..fd9619e 100755 --- a/src/jalview/gui/TreeCanvas.java +++ b/src/jalview/gui/TreeCanvas.java @@ -9,8 +9,9 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; +import java.awt.print.*; -public class TreeCanvas extends JPanel implements MouseListener +public class TreeCanvas extends JPanel implements MouseListener, Runnable, Printable { NJTree tree; JScrollPane scrollPane; @@ -266,10 +267,61 @@ public class TreeCanvas extends JPanel implements MouseListener } } + void startPrinting() + { + Thread thread = new Thread(this); + thread.start(); + } - public void paintComponent(Graphics g) { + // put printing in a thread to avoid painting problems + public void run() + { + PrinterJob printJob = PrinterJob.getPrinterJob(); + PageFormat pf = printJob.pageDialog(printJob.defaultPage()); + printJob.setPrintable(this, pf); + if (printJob.printDialog()) + { + try + { + printJob.print(); + } + catch (Exception PrintException) + { + PrintException.printStackTrace(); + } + } + } + public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException + { + font = new Font("Verdana",Font.PLAIN,fontSize); + pg.setFont(font); + FontMetrics fm = pg.getFontMetrics(font); + + pg.translate((int)pf.getImageableX(), (int)pf.getImageableY()); + + int pwidth = (int) pf.getImageableWidth(); + int pheight = (int) pf.getImageableHeight(); + + // adjust pheight to row height + pheight -= (pheight%fm.getHeight()); + pg.setClip(0,0, pwidth, pheight); + + int noPages = (fm.getHeight() * nameHash.size()) / pheight; + + if(pi>noPages) + return Printable.NO_SUCH_PAGE; + + pg.translate(0, -pheight*pi ); + draw(pg, pwidth, fm.getHeight() * nameHash.size()); + + return Printable.PAGE_EXISTS; + + } + + public void paintComponent(Graphics g) + { font = new Font("Verdana",Font.PLAIN,fontSize); g.setFont(font);