lunes, 9 de agosto de 2010

Imprimir componente de la pantalla

http://download.oracle.com/javase/tutorial/2d/printing/gui.html

http://download.oracle.com/javase/tutorial/2d/printing/dialog.html

---------------------

imprimir = new JButton("Print");
fechaPanel.add(imprimir);

imprimir.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(CalendarioWindows.this);

if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException exc) {
System.out.println(exc);
}
}
}

});
*********************

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}

Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());

/*escalar*/

String header = "Calendar View";
String header2 = getMesString(Integer.parseInt((String)mes.getSelectedItem())) + " " + anio.getValue();

Color original = g.getColor();

g.setColor(colorHeader);

g2d.setFont(fuenteHeader1);

g2d.drawString(header, 0, 30);

g2d.setFont(fuenteHeader2);

g2d.drawString(header2, 0, 56);

g2d.setColor(original);

g2d.translate(0, 80);

Double escala =calcularEscala( pf.getImageableWidth(), pf.getImageableHeight(),panelCalendario.getSize().getWidth(), panelCalendario.getSize().getHeight());
g2d.scale(escala, escala);

/* Print the entire visible contents of a java.awt.Frame */
panelCalendario.printAll(g);

return PAGE_EXISTS;

}

/**
* devuelve la escala necesaria pa que entre en la hoja
* @param paginaWidth
* @param paginaHeight
* @param graficoWidth
* @param graficoHeight
* @return
*/
public double calcularEscala(double paginaWidth, double paginaHeight,double graficoWidth, double graficoHeight){
double escala = 100 ;
System.out.println("**********************");
System.out.println("*** pw " +paginaWidth);
System.out.println("*** ph " +paginaHeight);
System.out.println("*** gw " +graficoWidth);
System.out.println("*** gh "+graficoHeight);

if(paginaWidth < graficoWidth){
escala= paginaWidth/graficoWidth;
System.out.println("** e1 "+ escala);

}
if(paginaHeight < graficoHeight){
double e = paginaHeight / graficoHeight;
System.out.println("** e2 " + e);
if(e escala = e;
}
}

System.out.println("** e3 "+ escala);
return escala;
}

No hay comentarios: