Java Print Dialog

The Java2D platform has the beginning of a robust printing model that is fully integrated with 2D graphics. We can easily print a drawing on a single sheet of paper. We can also manage a multipages printout and easily generate a print preview dialog.

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.awt.geom.*;

import java.awt.font.*;

import java.awt.print.*;

public class PrintTest

{

        public static void main(String aab[])

       {

         JFrame frame=new PrintTestFramee();

         frame.show();

     }

}

class PrintTestFramee extends JFrame implements ActionListener

{

     public PrintTestFramee()

     {

       setTitle("Printt Test");

       setSize(400,400);

       Container contentPanee=getContentPane();

     canvas=new PrintPanell();

     contentPanee.add(canvas,"Center");

     JPanel buttonPanel=new JPanel();

     printButton=new JButton("PrintPage");

     buttonPanel.add(printButton);

     printButton.addActionListener(this);

   pageeSetupButton=new JButton("PrintPage Setup");

    buttonPanel.add(pageeSetupButton);

    pageeSetupButton.addActionListener(this);

    contentPanee.add(buttonPanel,"North");

}

public void actionPerformed(ActionEvent acevt)

{

     Object source=acevt.getSource();

     if(source==printButton)

     {

         PrinterJob printJobb=PrinterJob.getPrinterJob();

         if(pageFormat==null)

       pageFormat=printJobb.defaultPage();

       printJobb.setPrintable(canvas,pageFormat);

     if(printJobb.printDialog())

       {

         try

         {

           printJobb.print();

         }

   catch(PrinterException pexcep)

       {

        JOptionPane.showMessageDialog(this,pexcep);

     }

}

}

else if(source==pageeSetupButton)

     {

   PrinterJob printJobb=PrinterJob.getPrinterJob();

   if(pageFormat==null)

     pageFormat=printJobb.defaultPage();

     pageFormat=printJobb.pageDialog(pageFormat);

   }

}

private JButton printButton;

private JButton pageeSetupButton;

private PrintPanell canvas;

private PageFormat pageFormat;

}

//To generate printout, u need to supply an object that implements the “printable” interface

class PrintPanell extends JPanel implements Printable

{

       public void paintComponent(Graphics Graap)

        {

          super.paintComponent(Graap);

          Graphics2D g2=(Graphics2D)Graap;

           drawPage(g2);

        }

/*To generate a printout, u need to start a print job.This method is called whenever the print engine need to have a page formatted for printing*/

public int print(Graphics g,PageFormat paf,int pagee) throws PrinterException

   {

     if(pagee>=1)

     return Printable.NO_SUCH_PAGE;

     Graphics2D g2=(Graphics2D)g;

     g2.setPaint(Color.black);

     g2.translate(paf.getImageableX(),paf.getImageableY());

     g2.draw(new Rectangle2D.Double(0,0,paf.getImageableWidth(),paf.getImageableHeight()));

    drawPage(g2);

    return Printable.PAGE_EXISTS;

 }

public void drawPage(Graphics2D g2)

     {

       FontRenderContext contextt=g2.getFontRenderContext();

     Font f=new Font("Seriff",Font.PLAIN,72);

     GeneralPath clipShapee=new GeneralPath();

     TextLayout layout=new TextLayout("XCnotes.com",f,contextt);

     AffineTransform transformm=AffineTransform.getTranslateInstance(0,72);

Shape outline=layout.getOutline(transformm);

   clipShapee.append(outline,false);

   g2.draw(clipShapee);

   g2.clip(clipShapee);

   final int NLINES=50;

   Point2D p=new Point2D.Double(0,0);

   for(int i=0;i<NLINES;i++)

     {

       double xx=(2*getWidth()*i)/NLINES;

     double yy=(2*getHeight()*(NLINES-1-i))/NLINES;

     Point2D q=new Point2D.Double(xx,yy);

     g2.draw(new Line2D.Double(p,q));

     }

   }

}        

OUTPUT...PRINTPAGE                                                                                                                            

Print Dialog

 

OUTPUT...PRINTPAGE SETUP

Print Page Setup

Explanation…

public int print(Graphics g,PageFormat paf,int pagee)

In this the page format tells you the paper size and the print margins. The page number tells you which page you need to render.

To start a print job, you use the PrinterJob class.First, you call the static getPrinterJob method to get a print job object.

Call the defaultPage method to obtain a default page format and then use the setPrintable method to set the Printable object that renders the page contents.

When you make a print job out of a Printable object, then the print job does not know how many pages you want to print. It simply keeps calling the print method,As long as the print method returns the value Printable.PAGE_EXISTS, the print job keeps producing pages. When the print method returns Printable.NO_SUCH_PAGE, the print job stops.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @