I have the following code. I want to print the canvas section where the user can draw shapes (square, line etc).
but I receive the following error once I try to print. How do I fix this error???Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.applet.*; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.RepaintManager; public class simple_draw extends Applet implements Printable { private Component componentToBePrinted; public static void printComponent(Component c) { new PrintUtilities(c).print(); } private static final long serialVersionUID = 1L; static int xx[] = new int[1024]; static int yy[] = new int[1024]; int n = 0; public void init() { setLayout(new BorderLayout(0,0)); JMenuBar bar = new JMenuBar(); JMenu menu = new JMenu("Print"); JMenuItem print = new JMenuItem("Print"); menu.setBackground(Color.lightGray); menu.setForeground(Color.black); bar.setBackground(Color.lightGray); menu.add(print); bar.add(menu); toolbox tb = new toolbox(); Panel cc = new Panel(); cc.setLayout(new BorderLayout(0,0)); cc.add("Center",new drawarea(tb)); Panel cm = new Panel(); cm.setBackground(Color.black); cm.setLayout(new BorderLayout(1,1)); cm.add("West",tb); cm.add("Center",cc); cm.add("North", bar); add("West",new blacksquare()); add("East",new blacksquare()); add("North",new blacksquare()); add("South",new blacksquare()); add("Center",cm); print.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { print(); } }); } public void print() { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable((Printable) this); if (printJob.printDialog()) try { System.out.println("Calling PrintJob.print()"); printJob.print(); System.out.println("End PrintJob.print()"); } catch (PrinterException pe) { System.out.println("Error printing: " + pe); } } public int print(Graphics g, PageFormat pf, int pageIndex) { int response = NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D) g; // for faster printing, turn off double buffering disableDoubleBuffering(componentToBePrinted); Dimension d = componentToBePrinted.getSize(); //get size of document double panelWidth = d.width; //width in pixels double panelHeight = d.height; //height in pixels double pageHeight = pf.getImageableHeight(); //height of printer page double pageWidth = pf.getImageableWidth(); //width of printer page double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); // make sure not print empty pages if (pageIndex >= totalNumPages) { response = NO_SUCH_PAGE; } else { // shift Graphic to line up with beginning of print-imageable region g2.translate(pf.getImageableX(), pf.getImageableY()); // shift Graphic to line up with beginning of next page to print g2.translate(0f, -pageIndex * pageHeight); // scale the page so the width fits... g2.scale(scale, scale); componentToBePrinted.paint(g2); //repaint the page for printing enableDoubleBuffering(componentToBePrinted); response = Printable.PAGE_EXISTS; } return response; } public static void disableDoubleBuffering(Component c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(false); } public static void enableDoubleBuffering(Component c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(true); } }
Calling PrintJob.print()
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at simple_draw.print(simple_draw.java:85)
at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1785)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1334)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1196)
at simple_draw.print(simple_draw.java:72)
at simple_draw$1.actionPerformed(simple_draw.java:61)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1000)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1041)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


Reply With Quote


Bookmarks