JPanel is bigger when printed than on screen
Hi, I am trying to do print a JPanel (send it to printer). I am having 2 issues,
1. JPanel become bigger than the size on screen. I have noticed it when I put
backgroud color on the said JPanel. Do I have to convert integer (width and height of JPanel) into Pixel?
2. Second issue is in printout the first page seems to have a margin, then second page follows the margin that I set in program.
Have read these tutorials:
Lesson: Printing (The Java™ Tutorials > 2D Graphics)
Chapter 6 Continued: Printing API
Here is a example app of what I am trying to do.
the JPanel I am trying to print
Code:
public class PnlPreview extends JPanel
{
JButton btnPrint;
public PnlPreview()
{
createUI();
}
private void createUI()
{
btnPrint = new JButton("PRINT");
btnPrint.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doPrint();
}
});
setLayout(new GridLayout(10, 10));
add(btnPrint);
}
private void doPrint()
{
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName("Printing Cheque..");
printJob.setCopies(1);
PrintPanel printFormat = new PrintPanel(this);
printJob.setPrintable(printFormat);
if(printJob.printDialog())
{
try
{
printJob.print();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
Font aFont = new Font("Serif", Font.BOLD, 12);
g2d.setFont(aFont);
g2d.drawString("October 5, 2011", 100, 100);
g2d.drawString("mine0926", 100, 200);
g2d.drawString("TRIAL", 100, 300);
}
}
Class that implements Printable
Code:
public class PrintPanel implements Printable
{
private JPanel panelToPrint;
public PrintPanel(JPanel panelToPrint)
{
this.panelToPrint = panelToPrint;
}
public int print(Graphics g, PageFormat pf, int PageIndex)
{
if(PageIndex > 1)
{
return NO_SUCH_PAGE;
}
Paper paper = new Paper();
double margin = 0;
paper.setImageableArea(margin, margin,
paper.getImageableWidth(),
paper.getImageableHeight());
pf.setPaper(paper);
//pf.setOrientation(PageFormat.PORTRAIT);
Graphics2D g2d = (Graphics2D)g;
g2d.translate(paper.getImageableX(), paper.getImageableY());
panelToPrint.print(g);
return PAGE_EXISTS;
}
}
Thanks in advance.
Re: JPanel is bigger when printed than on screen
Anyone who can point me in right direction?
Thanks..
Re: JPanel is bigger when printed than on screen
Quote:
Originally Posted by
mine0926
Anyone who can point me in right direction?
Thanks..
Your screen has a certain dpi (pixels per inch) and your printer probably has another dpi number; you have to scale( ... ) your g2d according to that ratio.
kind regards,
Jos
Re: JPanel is bigger when printed than on screen
How can I scale screen to printer's? do you have link? I actually experienced this in VB, so I try to search it on how to convert screen size to printer in java but I did not get any result that solve my problem, maybe I am using wrong keyword on search bar.
Thanks
Re: JPanel is bigger when printed than on screen
Quote:
Originally Posted by
mine0926
How can I scale screen to printer's? do you have link? I actually experienced this in VB, so I try to search it on how to convert screen size to printer in java but I did not get any result that solve my problem, maybe I am using wrong keyword on search bar.
Thanks
before you translate your image on the g2d printer Graphics2D, scale it: g2d.scale(r, r), where r is the ratio of the printer's dpi and the screen's dpi (I think the printer's dpi is 150, check your manuals).
kind regarrds,
Jos
Re: JPanel is bigger when printed than on screen
Thanks, That solved my problem. But I just manually set printers DPI, is there a way I can get and set the printer's DPI ?
I have seen examples but it uses eclipse library, is there any API in java that I can use in handling Printers?
Thanks again.
Re: JPanel is bigger when printed than on screen
Quote:
Originally Posted by
mine0926
Thanks, That solved my problem. But I just manually set printers DPI, is there a way I can get and set the printer's DPI ?
I have seen examples but it uses eclipse library, is there any API in java that I can use in handling Printers?
Thanks again.
Don't trust me on this, but I think I've read in the API documentation somewhere that the dpi for a printer is always 150. Correct me if I'm wrong.
kind regards,
Jos