Example usage for java.awt.print PrinterJob setPrintable

List of usage examples for java.awt.print PrinterJob setPrintable

Introduction

In this page you can find the example usage for java.awt.print PrinterJob setPrintable.

Prototype

public abstract void setPrintable(Printable painter);

Source Link

Document

Calls painter to render the pages.

Usage

From source file:Main.java

public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new Main());
    if (job.printDialog()) {
        try {//from   w ww .jav a  2 s.c  o m
            job.print();
        } catch (PrinterException e) {
        }
    }
    System.exit(0);
}

From source file:MainClass.java

public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();

    job.setPrintable(new PrintDemo1());

    job.setCopies(2);//from ww w .  j  a v a 2s .c  om
    job.setJobName("Printable");

    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }

    System.exit(0);
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//from  w  w  w. j  a  v a  2s .  com
        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setJobName("Graphics Demo Printout");
        pjob.setCopies(1);
        pjob.setPrintable(new Printable() {
            public int print(Graphics pg, PageFormat pf, int pageNum) {
                if (pageNum > 0) // we only print one page
                    return Printable.NO_SUCH_PAGE; // ie., end of job

                pg.drawString("www.java2s.com", 10, 10);

                return Printable.PAGE_EXISTS;
            }
        });

        if (pjob.printDialog() == false) // choose printer
            return;
        pjob.print();
    } catch (PrinterException pe) {
        pe.printStackTrace();
    }
}

From source file:com.google.code.facebook.graph.sna.applet.GraphEditorDemo.java

/**
 * a driver for this demo/*from w  w  w .  jav a 2s .  c  om*/
 */
@SuppressWarnings("serial")
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphEditorDemo demo = new GraphEditorDemo();

    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:external.jung.demo.GraphEditorDemo.java

/**
 * a driver for this demo/*  w ww  . j  av  a  2s.  c  om*/
 */
@SuppressWarnings("serial")
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphEditorDemo demo = new GraphEditorDemo();

    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {

        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {

        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:GraphEditorDemo.java

/**
 * a driver for this demo//from   www.j ava 2  s . c  o m
 */
@SuppressWarnings("serial")
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphEditorDemo demo = new GraphEditorDemo();

    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    menu.add(new AbstractAction("Save") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeTopologyFile(file);
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:jgraph.JShow.java

/**
 * a driver for this demo//from  w w w.j  a v a2s.c  om
 */
@SuppressWarnings("serial")
public static void showtest(DirectedOrderedSparseMultigraph<Object, Object> graph) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JShow demo = new JShow(graph);

    JMenu menu = new JMenu("Snapshot");
    menu.add(new AbstractAction("To JPEG") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:com.imag.nespros.gui.plugin.GraphEditor.java

private static void initMenu() {
    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }/*w w  w  .ja va  2s.  co  m*/
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    menu.add(new AbstractAction("Save topology") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                try {
                    demo.save(file);
                    frame.setTitle(file.getName());
                } catch (IOException ex) {
                    Logger.getLogger(GraphEditor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    menu.add(new AbstractAction("Load topology") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showOpenDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                try {
                    //EPGraph.getInstance().resetMapping();
                    simu.resetMapping();
                    demo.load(file);
                    frame.setTitle("Simulator - " + file.getName());
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(GraphEditor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    JMenu menu2 = new JMenu("View");
    menu2.add(new AbstractAction("Layout") {
        @Override
        public void actionPerformed(ActionEvent e) {
            Layout l = new CircleLayout<Device, ComLink>(Topology.getInstance().getGraph());
            l.setInitializer(vv.getGraphLayout());
            l.setSize(vv.getSize());
            LayoutTransition<Device, ComLink> lt = new LayoutTransition<>(vv, vv.getGraphLayout(), l);
            Animator animator = new Animator(lt);
            animator.start();
            vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
            vv.repaint();
        }
    });
    menu2.add(new AbstractAction("Event Composition Networks") {
        @Override
        public void actionPerformed(ActionEvent e) {
            showEPGraph(EPGraph.getInstance().getGraph());
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    menuBar.add(menu2);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
    buildEPGraphs();
    showEPGraph(EPGraph.getInstance().getGraph());
}

From source file:PrintUIWindow.java

public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    boolean ok = job.printDialog();
    if (ok) {//from w  w w . ja  va 2 s .c om
        try {
            job.print();
        } catch (PrinterException ex) {
            /* The job did not successfully complete */
        }
    }
}

From source file:PrintTest.java

public PrintTestFrame() {
    setTitle("PrintTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    canvas = new PrintComponent();
    add(canvas, BorderLayout.CENTER);

    attributes = new HashPrintRequestAttributeSet();

    JPanel buttonPanel = new JPanel();
    JButton printButton = new JButton("Print");
    buttonPanel.add(printButton);//from  w  w  w.j  a  v  a 2 s .c om
    printButton.addActionListener(new ActionListener() {
        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() {
        public void actionPerformed(ActionEvent event) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.pageDialog(attributes);
        }
    });

    add(buttonPanel, BorderLayout.NORTH);
}