0

I am trying to print a JPanel that is within an application, the JPanel inside the application is displayed below (Notice the horizontal and vertical scroll bars):

The Application When the application has run it's course, the height of the panel becomes very long, but when I print it, it does not print the entire width and height.

Also the positioning of the panel in the paper is align to the left rather than center. The Image below shows how the panel appears on paper, this looks very bad so please I need help.

1) How can I print the entire panel?

2) How can I position it to be centralized on the paper?

The Paper

Here is my code:

public void printWork()
{
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName(" Opt De Solver Printing ");

    pj.setPrintable(new Printable()
    {
        @Override
        public int print(Graphics pg, PageFormat pf, int pageNum)
        {
            if(pageNum > 0)
                return Printable.NO_SUCH_PAGE;

            Graphics2D g2 = (Graphics2D)pg;
            g2.translate(pf.getImageableX(), pf.getImageableY());
            MainAppPanel.this.paintAll(g2);
            /* I've tried the following commented codes but they don't work */
            //MainAppPanel.this.printAll(g2);
            //MainAppPanel.this.print(g2);
            //MainAppPanel.this.print(g2);
            return Printable.PAGE_EXISTS;
        }
    });
    if(pj.printDialog() == false)
        return;
    try
    {
        pj.print();
    }
    catch(PrinterException xcp)
    {
        xcp.printStackTrace(System.err);
    }
}
Jevison7x
  • 709
  • 2
  • 14
  • 31
  • Have a look at [this example](http://stackoverflow.com/questions/12764634/printing-a-jframe-and-its-components/12765916#12765916) and [this example](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22244116#22244116) and [this example](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22242658#22242658) and [this example](http://stackoverflow.com/questions/22058738/print-jlabels-icon-in-a-printer-using-a-button/22059079#22059079) – MadProgrammer Jun 05 '14 at 22:27
  • and [this example](http://stackoverflow.com/questions/17904518/fit-scale-jcomponent-to-page-being-printed/17961911#17961911) – MadProgrammer Jun 05 '14 at 22:27
  • Also, understand that changing the position and size of a "live" component can effect how it's painted on the screen – MadProgrammer Jun 05 '14 at 22:28
  • Please give me some time to consume the examples you gave me, I would be back to respond, thanks. @MadProgrammer – Jevison7x Jun 05 '14 at 22:37

1 Answers1

0

I Know this is 3 Year late now but for Reference:

The Positioning of the Image on the Page you have defined to the left uper corner here: g2.translate(pf.getImageableX(), pf.getImageableY());

So if you want to get it 100 Points to the right and 150 Points to the downside then you can do:

g2.translate(pf.getImageableX()+100, pf.getImageableY()+150);

Also if you want to scale the Image so it fits better use :

g2.scale(1,1);

Where 1 means 100% so 0.5 would mean 50% and 1.5 would be 150%