打印applet GUI

时间:2013-08-07 13:38:32

标签: java user-interface printing applet

我正在尝试打印我在applet GUI中的相同内容。

当我打印它时它不完全相同......每个组件的大小以不成比例的方式变化。

GUI:

enter image description here

印刷:

enter image description here

边距不是问题,我可以轻松修复它们......但是你可以看到条形码和垂直文字的尺寸不合适。

主要课程:

    public class Impresion extends Applet{
        PrinterJob job = PrinterJob.getPrinterJob();
        Panel test;
        Label test2;
        Label test3;
        String copyParam = null;
        String dialogParam = null;
        String codeParam = null;
        String descParam = null;
        public void init(){
            copyParam = this.getParameter("copias");
            dialogParam = this.getParameter("ventanita");
            codeParam = this.getParameter("codigo");
            descParam = this.getParameter("descripcion");
            copyParam = "1";
            dialogParam = "1";
            codeParam = "002/113";
            descParam = "asd";

            if (copyParam == null || dialogParam == null || codeParam == null || descParam == null){
                System.out.println("Faltan parametros. - "+copyParam+" - "+dialogParam+" - "+codeParam+" - "+descParam);
            } else {
                job.setPrintable(new Dibujar(codeParam, descParam));
                PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                float leftMargin = (float) 0.1;
                float topMargin = (float) 0.1;
                float rightMargin = (float) 0.1;
                float bottomMargin = (float) 0.1;
                float mediaWidth = (float) 60.0;
                float mediaHeight = (float) 50.0;
                aset.add(new MediaPrintableArea(leftMargin, topMargin, (mediaWidth - leftMargin - rightMargin), (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));
                int copias = Integer.parseInt(copyParam);
                aset.add(new Copies(copias));
                boolean imprimir = true;
                if (dialogParam.indexOf('1') != -1){
                    imprimir = job.printDialog(aset);
                }
                if (imprimir){
                    try {
                        job.print(aset);
                    } catch (PrinterException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

可打印课程:

    public class Dibujar implements Printable{
        String codeParam = null;
        String descParam = null;
        public Dibujar(String code, String desc) {
            codeParam = code;
            descParam = desc;
        }

        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException{
            if (pageIndex > 0){
                return NO_SUCH_PAGE;
            }
            Barcode b = null;
            try {
                b = BarcodeFactory.createCodabar(codeParam);
                b.setDrawingText(false);
                b.setBarWidth(1);
                b.draw((Graphics2D) g, 30, 8);
            } catch (BarcodeException | OutputException e1) {
                e1.printStackTrace();
            }
            g.setFont(new Font("Arial", Font.BOLD, 9));
            String description = descParam;
            g.drawString(description, 16, 70);
            AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(90),16,8);
            Graphics2D g2 = (Graphics2D) g;
            g2.transform(at);
            g.setFont(new Font("Arial", Font.BOLD, 14));
            g2.drawString(codeParam.replace("/",  ""),16,8);
            g2.transform(new AffineTransform());
            return PAGE_EXISTS;
        }
    }

1 个答案:

答案 0 :(得分:0)

调整参数:

aset.add(new MediaPrintableArea(leftMargin, topMargin, (mediaWidth - leftMargin - rightMargin), (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));

或者调整这两个参数:

b.draw((Graphics2D) g, 30, 8);
//....
g2.drawString(codeParam.replace("/", ""),16,8);