简体   繁体   中英

How to print jpanel with graphic components

Here i use just print the JPanel but i need to put graphics component for drawLine method. how to use it anyone please advice me. my sample code for print JPanel here.

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
    }
}

Add

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

Replacing ( or assigning to ) the four parameters with integer values you want to use. eg:

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

Should work for most displays.

g2.setColor(r,g,b);

Before, if you want other than black.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM