JComponent Printing

I need to be able to print JComponent in such a way that it looks amazing. Do not ask me, which is surprising, since I do not know either.

JComponent must be too large for the page in x and y format. I need to print the same JComponent on many pages, divided into a given y coordinate.

I tried just scaling up the JComponent, although I get problems to select the given y coordinate and make it look good.

I tried to change the JComponent to the given size, although then the given y coordinate is problematic, the JComponent does not look so good, and I have a lot of problems if the program is changed.

So, now I am sitting here and do not know what to do, because there seems to be a complete lack of textbooks on the Internet on the topic ...

How do Word and other text programs do?

Regards, Skarion

+1
source share
1 answer

This is a common problem, I have to say that I have encountered it already. The following lessons were useful to me:

  • Tutorial1 --- It describes a problem showing how the page looks, etc.

  • Tutorial2 --- Here you have a good working code example that I used to solve my problems. The author also suggests the use of scaling, translation, etc. For print. A.

I hope this helps too.

Maybe when you learn more about what awesome means in your problem, I can help more. :)

EDIT1:

    double scale = 1;
    //scale only when component is wider then a page (page and component widths are doubles)
    if(componentWidth > pageWidth)
        scale = pageWidth/ componentWidth;
    //I first calculate where each page should end 
    //...
    //then when I paint a page I calculate translation over Y for each page
    double translateY = 0;
    //if page index grater then zero then take where the previous page ends
    if(pageIndex > 0)
        translateY = pageHeightEnds.get(pageIndex - 1);
    //shift Graphic to line up with beginning of next page to print
    g2.translate(0f, -translateY);

    g2.setClip(0, (int) Math.round(translateY),(int) Math.round(pageWidth), (int) Math.round(currentPageWidth));
    //  scale the page so the width fits...
    g2.scale(scala, scala);
    componentToPaint.paint(g2); 

Good luck, Borough.

+4
source