Wednesday, 7 August 2013

Printing applet GUI - JAVA

Printing applet GUI - JAVA

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: http://i.imgur.com/FoDCzUA.png
Printed: http://i.imgur.com/yw2kol4.png
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;
}
}

1 comment:

  1. Good job,i learn a lot,thanks.
    I have a Java applet that calculates the binomial expansion, however when the string is long the string overflows the applet and you can only see a portion of it, how can I fix this, so i have to add a scroll bar or what would be the easiest thing to do? I use :g.drawString(ans ,10,100);
    print java applet barcode

    ReplyDelete