import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Date;
import java.util.logging.*;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;
import net.sourceforge.barbecue.*;
import net.sourceforge.barbecue.output.OutputException;
public class imprimir_caja {
int count=0;
String iden, country, country2, qty, cust_num
; boolean band=false;
String item_no,ident,country_o,country_o2,qty2,cus
; imprimir_caja obj=new imprimir_caja();
obj.impresion();
}
Class.forName("com.mysql.jdbc.Driver");
String url
= "jdbc:mysql://localhost/etiquetas"; }
con = getConnection();
rs = smt.executeQuery("consulta sql");
rs.first();
item = rs.getString("item");
iden = rs.getString("identifier");
country = rs.getString("country_origin");
country2 = rs.getString("country2");
qty = rs.getString("qty");
cust_num = rs.getString("customer_num");
book.append(new IntroPage(), printJob.defaultPage());
printJob.setPageable(book);
int count = 0;
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);//con este vemos las impresoras instaladas como mis pruebas estan en red pues por eso uso este
for (PrintService printService : services) {//este for se quita
if (count == 1) {//junto con esto
printName = printService.getName();//si se le da el nombre a la impresora cambia por printName="nombre impresora"
}//esto
count = count + 1;//esto
}//y esto si se quiere dar el nombre de la impresora
aset.add(new PrinterName(printName, null));
services = PrintServiceLookup.lookupPrintServices(null, aset);//busca la impresora
for (PrintService printService : services) {
PrintService printers[] = PrintServiceLookup.lookupPrintServices(null, aset);
if (printers.length == 1) {
printJob.setPrintService(printers[0]);//le asignamos la impresora de trabajo al job
try {
printJob.print();//imprimimos lo q esta en el job
PrintException.printStackTrace();
}
}
}
}
private class IntroPage
implements Printable { try {
int iNumero=fecha.getYear();
int anio = iNumero%100;
int m= fecha.getMonth()+1;
if(mes.length()==1){
mes="0"+mes;
}
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.
setPaint(Color.
black); g2d.draw(rec_item);
g2d.draw(rec_q);
g2d.draw(rec_l);
g2d.draw(rec_c);
String country
= "HARADA INDUSTRY OF AMERICA"; System.
out.
println("el valor de itemn: "+itemn
); System.
out.
println("el valor de item3: "+item3
); Barcode barcode = BarcodeFactory.createCode39(item3, false);
barcode.setDrawingText(false);
barcode.setBarHeight(45);
barcode.setBarWidth(1);
Barcode barcodeqty = BarcodeFactory.createCode39(qtyn, false);
barcodeqty.setDrawingText(false);
barcodeqty.setBarHeight(42);
barcodeqty.setBarWidth(1);
BufferedImage imageqty
= BarcodeImageHandler.
getImage(barcodeqty
); julian="L"+julian;
Barcode barcodelot = BarcodeFactory.createCode39(julian, false);
barcodelot.setDrawingText(false);
barcodelot.setBarHeight(42);
barcodelot.setBarWidth(1);
BufferedImage imagelot
= BarcodeImageHandler.
getImage(barcodelot
); String country2
= "ASSEMBLED IN MEXICO"; g2d.setFont(countryFont);
g2d.drawString(country, 51, 12);
g2d.setFont(itemFont);
g2d.drawString(item2, 6, 24);
g2d.setFont(nFont);
g2d.drawString(n, 15, 33);
g2d.setFont(itemnFont);
g2d.drawString(itemn, 82, 32);
g2d.drawImage(image, 18, 37, null);
g2d.setFont(qualityFont);
g2d.drawString(quality, 6, 93);
g2d.setFont(qFont);
g2d.drawString(q, 15, 102);
g2d.setFont(qtyFont);
g2d.drawString(qty, 115, 96);
g2d.drawImage(imageqty, 86, 97, null);
g2d.setFont(loteFont);
g2d.drawString(lote, 6, 150);
g2d.setFont(lFont);
g2d.drawString(l, 12, 159);
g2d.setFont(lotFont);
g2d.drawString(lotn, 109, 152);
g2d.drawImage(imagelot, 83, 153, null);
g2d.setFont(cusFont);
g2d.drawString(cus, 6, 204);
g2d.setFont(cus_nFont);
g2d.drawString(cust_n, 80, 209);
g2d.setFont(country2Font);
FontMetrics country2fontMetrics
= g2d.
getFontMetrics(); g2d.drawString(country2, 76, 225);
} catch (OutputException ex) {
Logger.getLogger(imprimir_caja.class.getName()).log(Level.SEVERE, null, ex);
} catch (BarcodeException ex) {
Logger.getLogger(imprimir_caja.class.getName()).log(Level.SEVERE, null, ex);
} return PAGE_EXISTS;
}
}
}