打印JComponent [英] Printing JComponent
问题描述
我需要能够以某种方式打印JComponent,因此它看起来很棒。不要问我有什么可怕的,因为我不知道。
I am required to be able to print a JComponent in a fashion so it looks awesome. Don't ask me what awesome is as I don't know either.
对于x和y格式的页面,JComponent都要求太大。我需要将相同的JComponent打印到在给定y坐标处分割的许多页面中。
The JComponent is required to be too big for a page both in a x and y format. I am required to print the same JComponent into many pages split at a given y coordinate.
我尝试过缩放JComponent,但是在选择给定的y坐标时遇到问题并使它看起来很好。
I have tried just scale the JComponent, though get problems to select a given y coordinate and to make it look good.
我试图将JComponent更改为给定的大小,但是给定的y坐标有问题,JComponent看起来不那么好,我遇到很多问题如果程序被更改了。
I have tried to change the JComponent to a given size, though then a given y coordinate is problematic, the JComponent don't look so good and I get lots of problems if the program is changed.
所以现在我坐在这里,不知道该怎么做,因为似乎完全缺乏关于该主题的在线教程.. 。
So now I sit here and don't know what to do as there seems to be a total lack of tutorials online on the topic...
Word和其他基于文本的程序如何实现这一目标?
How do Word and other text based programs accomplish this?
祝你好运,
Skarion
Best regards, Skarion
推荐答案
这是我必须说的一个常见问题。
这些教程对我有用:
This is a common problem I must say I faced it already. Useful for me were these tutorials:
-
Tutorial1 ---它描述的问题显示了页面的外观等。
Tutorial1 --- It describes the problem shows how a page looks like etc.
Tutorial2 ---这里有一个很好的工作代码示例,我用它来解决我的问题。作者还介绍了缩放,翻译等用于打印的内容。
Tutorial2 --- Here you have nice working code example, which I have used in solving my problems. The author also presents use of scaling, translating, etc. for printing.
我希望它也会对你有帮助。
I hope it will help you too.
也许当你更了解你的问题意味着什么很棒时,我可以提供更多帮助。 :)
Maybe when you know more about what in your problem means awesome, 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);
祝你好运,博罗。
这篇关于打印JComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!