简体   繁体   中英

Printing applet GUI

I'm trying to print the same that I have in my applet GUI.

The things it's that when I print it it's not exactly the same... the size of each component changes in a disproportionately way.

The GUI:

在此处输入图片说明

Printed:

在此处输入图片说明

The margins are not a problem, I can fix them easily... but as you can see the barcode and the vertical text has not the proper size.

Main class:

    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();
                    }
                }
            }
        }
    }

Printable class:

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

Adjust the parameters in this:

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

Or adjust the parameters in these two:

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

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