Newsgroup: it.comp.java
Titolo: STAMPARE UNA TEXT AREA. MA PER FORMATTARE???

Salve a tutti!
qualcuno puo' spiegarmi, anche con qualche esempio come riesco a stampare
PIPPO al centro di un foglio!!!
Riesco a stampare una textArea... ma ho interesse a formattare la stampa.
vi allego il mio codice (funzionante per una TextArea), per rispondere a
qualcuno che lo ha chiesto.
Come modifico questo codice per stampare una stringa al centro??
Il metodo testPrint viene chiamato da un generico evento (tipo pressione di
un tasto).
 public void testPrint()
   int m_pages = 1
   PrinterJob pj = PrinterJob.getPrinterJob();
   PageFormat pf = pj.defaultPage();
   pj.setPrintable(this,pf);
   if (!pj.printDialog())
   return;
   try {
   pj.print();
   }
  catch(Exception e) {
  e.printStackTrace(); }
  }
  public int print(Graphics g, PageFormat pf, int pageIndex) throws
PrinterException
  {
    if (pageIndex >= 1)
    return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D)g;
    g2.translate(pf.getImageableX(), pf.getImageableY());
    paint(g2);
    System.gc();
    return Printable.PAGE_EXISTS;
  }

 wrote in message
news:8hjtns$era$1...@nslave1.tin.it...

import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;
public class ProvaStampa extends JPanel implements ActionListener{
   final static JButton button = new JButton("Print");
   public ProvaStampa() {
        button.addActionListener(this);
   }
  public void actionPerformed(ActionEvent e)
  {
    // Get a PrinterJob
    PrinterJob job = PrinterJob.getPrinterJob();
    // Set a Printable
    job.setPrintable(new PaintContent(), job.defaultPage());
  if (job.printDialog())
  {
      try
          job.print();
      }
      catch (Exception exc)
          exc.printStackTrace();
      }
 }
}
  public static void main(String[] args) {
        WindowListener l = new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
                public void windowClosed(WindowEvent e) {System.exit(0);}
        };
        JFrame f = new JFrame();
        f.addWindowListener(l);
        JPanel panel = new JPanel();
        panel.add(button);
        f.getContentPane().add(BorderLayout.SOUTH, panel);
        f.getContentPane().add(BorderLayout.CENTER, new ProvaStampa());
        f.setSize(775, 450);
        f.pack();
        f.show();
  }
}
class PaintContent implements Printable
{
  Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 10);
  public int print(Graphics g, PageFormat pf, int pageIndex)
        throws PrinterException
  {
    int NumPagina = pageIndex + 1;
    // Per printable
    if(pageIndex > 0) return(Printable.NO_SUCH_PAGE);
    System.out.println("PaintContent In "+pageIndex);
    System.out.println("Width: "+pf.getWidth()+ " Height: "+
                        pf.getHeight());
    System.out.println("PrintWidth: "+pf.getImageableWidth()+
                       " PrintHeight: "+pf.getImageableHeight());
    System.out.println("X0: "+pf.getImageableX()+
                       " Y0: "+pf.getImageableY());
    Graphics2D g2 = (Graphics2D)g;
    g2.setPaint(Color.white);
    g2.drawRect(80,80,600,600);
    g2.setPaint(Color.black);
    g2.translate(pf.getImageableX(),pf.getImageableY());
    g2.setFont(fnt);
    g2.setColor(Color.red);
    g2.drawString("Pagina: "+NumPagina, 100, 200);
    g2.setColor(Color.blue);
    g2.drawString("CORPO", 0, 0);
    g2.drawString("CORPO1", 0, 20);
    g2.drawString("CORPO2", 0, 40);
    g2.drawString("CORPO3", 0, 60);
    System.gc();
    System.out.println("PaintContent Exit");
    return Printable.PAGE_EXISTS;
  }
}