首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印JComponent

打印JComponent
EN

Stack Overflow用户
提问于 2011-04-17 17:53:47
回答 1查看 1.4K关注 0票数 1

我被要求能够打印一个时尚的JComponent,所以它看起来很棒。不要问我什么是最棒的,因为我也不知道。

对于x和y格式的页面,JComponent都要求太大。我需要将相同的JComponent打印到许多在给定y坐标上拆分的页面中。

我已经尝试过缩放JComponent,但在选择给定的y坐标并使其看起来更好时遇到了问题。

我尝试将JComponent更改为给定的大小,但是给定的y坐标是有问题的,JComponent看起来不是很好,如果程序更改,我会遇到很多问题。

所以现在我坐在这里,不知道该做什么,因为网上似乎完全缺乏关于这个主题的教程……

Word和其他基于文本的程序是如何做到这一点的?

致以最好的问候,Skarion

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-17 18:15:24

这是一个常见的问题,我必须说我已经面对过它了。这些教程对我很有用:

  1. Tutorial1 -它描述了问题,显示了页面看起来像etc.
  2. Tutorial2 -这里有很好的工作代码示例,我用它来解决我的问题。作者还介绍了用于打印的缩放、转换等功能。

我希望它也能帮助你。

也许当你对你的问题有更多了解的时候,我可以提供更多的帮助。:)

EDIT1:

代码语言:javascript
复制
    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); 

祝你好运,波罗。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5692823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档