Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 726421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:27:00+00:00 2026-05-14T06:27:00+00:00

I’m trying to print a JPanel with some painted graphics on it (overriding paintComponent).

  • 0

I’m trying to print a JPanel with some painted graphics on it (overriding paintComponent). The graphics is so big that they wont fit on a single page and therefor I’m letting it span across multiple pages. My problem lies within the fact that if I let the user choose the pageFormat/Paper type by calling:

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PageFormat pf = printJob.pageDialog(aset);
printJob.setPrintable(canvas, pf);

When I’m writing my print() method (implementing Printable) in my JPanel class I can’t seem to get the hold of the margins? I use graphics.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); to make it start drawing in the correct topleft corner (0;0) and it takes the margins into consideration (i.e.,starting more at (80; 100) or so). But then it prints over the bottom and right margin which I don’t want it to do since that negates the user’s wishes.

Here is the code of my print() method as a reference, which works fine when you don’t let the user set the paper (using the default instead):

    Rectangle[] pageBreaks;

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        //Calculate how many pages our print will be
        if(pageBreaks == null){
            double pageWidth = pageFormat.getPaper().getWidth();
            double pageHeight = pageFormat.getPaper().getHeight();

            //Find out how many pages we need
            int numberOfPagesHigh = (int) Math.ceil(size.getHeight()/pageHeight);
            int numberOfPagesWide = (int) Math.ceil(size.getWidth()/pageWidth); 
            pageBreaks = new Rectangle[numberOfPagesHigh*numberOfPagesWide];

            double x = 0;
            double y = 0;
            int curXPage = 0;

            //Calculate what we will print on each page
            for (int i = 0; i < pageBreaks.length; i++){
                double xStart = x;
                double yStart = y;
                x += pageWidth;

                pageBreaks[i] = new Rectangle((int)xStart, (int)yStart, (int)pageWidth, (int)pageHeight);

                curXPage++;
                if (curXPage > numberOfPagesWide){
                    curXPage = 0;
                    x = 0;
                    y += pageHeight;
                }
            }


        }

        if (pageIndex < pageBreaks.length){
            //Cast graphics to Graphics2D for richer API
            Graphics2D g2d = (Graphics2D) graphics;

            //Translate into position of the paper
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

            //Setup our current page
            Rectangle rect = pageBreaks[pageIndex];

            g2d.translate(-rect.x, -rect.y);
            g2d.setClip(rect.x, rect.y, rect.width, rect.height);

            //Paint the component on the graphics object
            Color oldBG = this.getBackground();
            this.setBackground(Color.white);
            util.PrintUtilities.disableDoubleBuffering(this);
            this.paintComponent(g2d);
            util.PrintUtilities.enableDoubleBuffering(this);
            this.setBackground(oldBG);

            //Return
            return PAGE_EXISTS;
        }
        else {
            return NO_SUCH_PAGE;
        }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:27:01+00:00Added an answer on May 14, 2026 at 6:27 am

    After posting this question and tabbing back into my IDE I pretty easily found the answer. Instead of using

    double pageWidth = pageFormat.getPaper().getWidth();
    double pageHeight = pageFormat.getPaper().getHeight(); 
    

    use

    double pageWidth = pageFormat.getImageableWidth();
    double pageHeight = pageFormat.getImageableHeight();
    

    The getImageableWidth() returns the totalPaperWidth-totalMargins whereas getWidth() just returns totalPaperWidth. This makes the print() method not draw more that it can on each page!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.