Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package impressoraApplet; |
| 2 | |||
| 3 | import java.awt.Font; |
||
| 4 | import java.awt.Graphics; |
||
| 5 | import java.awt.Graphics2D; |
||
| 6 | import java.awt.print.Book; |
||
| 7 | import java.awt.print.PageFormat; |
||
| 8 | import java.awt.print.Paper; |
||
| 9 | import java.awt.print.Printable; |
||
| 10 | import java.awt.print.PrinterException; |
||
| 11 | import java.awt.print.PrinterJob; |
||
| 12 | import javax.swing.JApplet; |
||
| 13 | |||
| 14 | public class imprime extends JApplet implements Printable { |
||
| 15 | |||
| 16 | private Book book; |
||
| 17 | public double fatorConverMMPt = 2.834646D; |
||
| 18 | |||
| 19 | //========== - Metodo de iniciação do applet - ============= |
||
| 20 | @Override |
||
| 21 | public void init() { |
||
| 22 | imprimir(); |
||
| 23 | } |
||
| 24 | |||
| 25 | // public static void main(String[] args) { |
||
| 26 | // Imprime imprime = new Imprime(); |
||
| 27 | // imprime.imprimir(); |
||
| 28 | // } |
||
| 29 | |||
| 30 | //========== - Metodo de configuração da impressora Padrão - ============= |
||
| 31 | public void imprimir() { |
||
| 32 | PrinterJob impressoraPadrao = PrinterJob.getPrinterJob(); |
||
| 33 | //p.defaultPage(); |
||
| 34 | impressoraPadrao.setPrintable(this); |
||
| 35 | book = new Book(); |
||
| 36 | PageFormat pageFormat = new PageFormat(); |
||
| 37 | //pageFormat = printJob.pageDialog(pageFormat); |
||
| 38 | Paper Folha = new Paper(); |
||
| 39 | //Papel A4 |
||
| 40 | //8,5 pol. |
||
| 41 | double width = 210.9 * fatorConverMMPt; |
||
| 42 | //13 pol. |
||
| 43 | double height = 214.9 * fatorConverMMPt; |
||
| 44 | //double width = 8.2; |
||
| 45 | //double height = 8.2; |
||
| 46 | //System.out.print("Largura: " + width + " Altura: " + height); |
||
| 47 | Folha.setSize(width, height); |
||
| 48 | Folha.setImageableArea(0.0, 0.0, width, height); |
||
| 49 | pageFormat.setPaper(Folha); |
||
| 50 | book.append(this, pageFormat); |
||
| 51 | impressoraPadrao.setPageable(book); |
||
| 52 | try { |
||
| 53 | impressoraPadrao.print(); |
||
| 54 | } catch (Exception e) { |
||
| 55 | e.printStackTrace(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | /** Método da interface Printable */ |
||
| 60 | public int print(Graphics g, PageFormat format, int pageIndex) throws PrinterException { |
||
| 61 | |||
| 62 | Graphics2D eventoGrafico = (Graphics2D) g; |
||
| 63 | |||
| 64 | //==== - Define o formado da folha de impressão (area possivel de se fazer a impressão do conteudo) - ======= |
||
| 65 | eventoGrafico.translate(format.getImageableX(), format.getImageableX()); |
||
| 66 | |||
| 67 | //======- Definição da font a ser utilizada - ================ |
||
| 68 | eventoGrafico.setFont(new Font("Sans Serif", Font.PLAIN, 8 )); |
||
| 69 | |||
| 70 | //======- Imprimindo o conteudo evento.drawString(valor, eixo x, e eixo y) - ================ |
||
| 71 | |||
| 72 | |||
| 73 | eventoGrafico.drawString("Texto a ser impresso1", 46, 169); |
||
| 74 | eventoGrafico.drawString("Texto a ser impresso2", 46, 170); |
||
| 75 | |||
| 76 | return PAGE_EXISTS; |
||
| 77 | } |
||
| 78 | } |