如何使用图形组件打印 jpanel



在这里我只使用打印 JPanel,但我需要为绘图线方法放置图形组件。 如何使用它,任何人请告诉我。 我在这里打印 JPanel 的示例代码。

public class Sample {
JPanel component_name = new JPanel();   
public void printComponenet(){
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName(" Print Component ");
    pj.setPrintable (new Printable() {    
        public int print(Graphics pg, PageFormat pf, int pageNum){
            if (pageNum > 0){
                return Printable.NO_SUCH_PAGE;
            }
            Graphics2D g2 = (Graphics2D) pg;
            g2.translate(pf.getImageableX(), pf.getImageableY());
            component_name.printAll(g2);
            return Printable.PAGE_EXISTS;
        }
    });
    if (pj.printDialog() == false)
        return;
    try {
        pj.print();
    } catch (PrinterException ex) {
        // handle exception
    }
}

添加

g2.drawLine(left,top,width,height);

将四个参数替换为要使用的整数值。 例如:

g2.drawLine(30,40,400,300); 

应该适用于大多数显示器。

g2.setColor(r,g,b);

以前,如果你想要黑色以外的其他。

最新更新