相关疑难解决方法(0)

将JComponent调整/缩放到要打印的页面

我正在尝试缩放我的组件,以便它可以放在单个打印页面(纵向或横向)上

 gDiagram.getComponent()
Run Code Online (Sandbox Code Playgroud)

是我试图打印的组件(JPanel).

这是我到目前为止基于如何打印单个JPanel内容的内容?

/**
 * Prints the diagram.
 */
public void printDiagram() {
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName(" Print Component ");

    pj.setPrintable(new Printable() {
        @Override
        public int print(Graphics g, PageFormat pf, int pageNumber)
                throws PrinterException {
            // TODO Auto-generated method stub
            if (pageNumber > 0) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2 = (Graphics2D) g;
            g2.translate(pf.getImageableX(), pf.getImageableY());

            double sx = pf.getImageableWidth() / gDiagram.getComponent().getWidth();
            double sy = pf.getImageableHeight() / gDiagram.getComponent().getHeight();

            gDiagram.getComponent().paint(g2);
            g2.scale(sx, sy);
            return Printable.PAGE_EXISTS;
        }
    });

    if (!pj.printDialog()) { …
Run Code Online (Sandbox Code Playgroud)

java printing swing graphics2d

3
推荐指数
1
解决办法
5620
查看次数

标签 统计

graphics2d ×1

java ×1

printing ×1

swing ×1