Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: JPanel is bigger when printed than on screen

  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default 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
    Java 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
    Java 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.
    Last edited by mine0926; 10-05-2011 at 06:10 AM. Reason: Fix code tags

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: JPanel is bigger when printed than on screen

    Anyone who can point me in right direction?

    Thanks..

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: JPanel is bigger when printed than on screen

    Quote Originally Posted by mine0926 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default 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

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: JPanel is bigger when printed than on screen

    Quote Originally Posted by mine0926 View Post
    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
    mine0926 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: JPanel is bigger when printed than on screen

    Quote Originally Posted by mine0926 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Applet: drawing window bigger than screen
    By srbehera in forum Java Applets
    Replies: 2
    Last Post: 04-18-2011, 04:48 AM
  2. Replies: 11
    Last Post: 03-09-2011, 08:18 AM
  3. iReport - Print on page bigger than A4
    By mobydickJavaCoder in forum Advanced Java
    Replies: 1
    Last Post: 05-26-2010, 10:55 AM
  4. project bigger then me
    By mac in forum New To Java
    Replies: 2
    Last Post: 02-19-2010, 02:14 PM
  5. bigger than long
    By scorp200 in forum New To Java
    Replies: 3
    Last Post: 12-06-2009, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •