import java.awt.*;\r
import java.awt.event.*;\r
import java.util.*;\r
+import java.awt.print.*;\r
\r
-public class TreeCanvas extends JPanel implements MouseListener\r
+public class TreeCanvas extends JPanel implements MouseListener, Runnable, Printable\r
{\r
NJTree tree;\r
JScrollPane scrollPane;\r
}\r
}\r
\r
+ void startPrinting()\r
+ {\r
+ Thread thread = new Thread(this);\r
+ thread.start();\r
+ }\r
\r
- public void paintComponent(Graphics g) {\r
+ // put printing in a thread to avoid painting problems\r
+ public void run()\r
+ {\r
+ PrinterJob printJob = PrinterJob.getPrinterJob();\r
+ PageFormat pf = printJob.pageDialog(printJob.defaultPage());\r
+ printJob.setPrintable(this, pf);\r
+ if (printJob.printDialog())\r
+ {\r
+ try\r
+ {\r
+ printJob.print();\r
+ }\r
+ catch (Exception PrintException)\r
+ {\r
+ PrintException.printStackTrace();\r
+ }\r
+ }\r
+ }\r
\r
\r
+ public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException\r
+ {\r
+ font = new Font("Verdana",Font.PLAIN,fontSize);\r
+ pg.setFont(font);\r
+ FontMetrics fm = pg.getFontMetrics(font);\r
+\r
+ pg.translate((int)pf.getImageableX(), (int)pf.getImageableY());\r
+\r
+ int pwidth = (int) pf.getImageableWidth();\r
+ int pheight = (int) pf.getImageableHeight();\r
+\r
+ // adjust pheight to row height\r
+ pheight -= (pheight%fm.getHeight());\r
+ pg.setClip(0,0, pwidth, pheight);\r
+\r
+ int noPages = (fm.getHeight() * nameHash.size()) / pheight;\r
+\r
+ if(pi>noPages)\r
+ return Printable.NO_SUCH_PAGE;\r
+\r
+ pg.translate(0, -pheight*pi );\r
+ draw(pg, pwidth, fm.getHeight() * nameHash.size());\r
+\r
+ return Printable.PAGE_EXISTS;\r
+\r
+ }\r
+\r
+ public void paintComponent(Graphics g)\r
+ {\r
font = new Font("Verdana",Font.PLAIN,fontSize);\r
g.setFont(font);\r
\r